📜  如何在Python使用 strftime() 格式化日期?

📅  最后修改于: 2022-05-13 01:55:11.287000             🧑  作者: Mango

如何在Python使用 strftime() 格式化日期?

在本文中,我们将看到如何在Python使用 strftime() 格式化日期。 localtime() 和 gmtime() 返回一个表示时间的元组,这个元组被转换为一个字符串,如格式参数使用Python时间方法 strftime() 指定的那样。

句法:

Directive or format code Returned ValuedExample
%YFull year with century2021,2022
%yYear without century with zero padded value00,01,….21,22…,99
%-yYear without century0,1…,99
%mMonth with zero padded value01-12
%-mMonth without zero padded value1-12
%BFull month nameJanuary, February,…, December
%bShort form of month Jan, Feb,…,Dec
%AFull weekday nameSunday, Monday,..
%aShort form of weekday nameSun, Mon,..
%wWeekday as decimal value0-6
%dDays with zero padded value01-31
%-dDays with decimal value1-31
%HHour (24-hour clock) as a zero-padded value.00-23
%-HHour (24-hour clock) without zero-padded value.0,1,…,23
%IHour (12-hour clock) as a zero-padded value.01-12
%-IHour (12-hour clock) without zero-padded value.1-12
%MMins with zero-padded 00-59
%-MMins without zero padded value0-59
%SSecs with zero padded value00-59
%-SSecs without zero padded value0-59
%fMicro Secs with zero-padded value000000 – 999999
%pLocale’s AM or PM.AM/PM
%jDay of the year with zero padded value001-366
%-jDay of the year without zero padded value1-366
%zUTC offset in the form +HHMM or -HHMM. 
%ZTime zone name. 
%CLocale’s appropriate date and timeFri Apr 02 02:09:07 2020
%xLocale’s appropriate date02/04/22
%XLocale’s appropriate time02:04:22
%WWeek number of the year. Monday as first day of week00-53
%UWeek number of the year. Sunday as first day of week00-53

下面是一些示例,以便更好地理解。

示例 1:

Python3
from datetime import datetime
  
# current time and date
# datetime object
time = datetime.now()
print("Without formating:", time)
  
# formating date using strftime
print("After formating:", time.strftime("%b %d, %Y"))


Python3
from datetime import datetime
  
# current time and date
# datetime object
time = datetime.now()
print("Without formating:", time)
  
# formating date using strftime
print("Year", time.strftime("%Y"))
print("Month name", time.strftime("%B"))
print("Day", time.strftime("%d"))


Python3
from datetime import datetime
  
# current time and date
# datetime object
time = datetime.now()
  
# formating date using strftime
# format = MM/DD/YY
print(time.strftime("%m/%d/%y"))
  
# format = Month D, Yr
print(time.strftime("%B %d, %Y"))
  
# time formating
# HH:MM:SS
print(time.strftime("%H:%M:%S"))


输出:

示例 2:

蟒蛇3

from datetime import datetime
  
# current time and date
# datetime object
time = datetime.now()
print("Without formating:", time)
  
# formating date using strftime
print("Year", time.strftime("%Y"))
print("Month name", time.strftime("%B"))
print("Day", time.strftime("%d"))

输出:

示例 3:

蟒蛇3

from datetime import datetime
  
# current time and date
# datetime object
time = datetime.now()
  
# formating date using strftime
# format = MM/DD/YY
print(time.strftime("%m/%d/%y"))
  
# format = Month D, Yr
print(time.strftime("%B %d, %Y"))
  
# time formating
# HH:MM:SS
print(time.strftime("%H:%M:%S"))

输出: