Python datetime strftime()方法与示例

Python datetime.strftime()方法 (Python datetime.strftime() Method)

datetime.strftime() method is used to manipulate objects of datetime class of module datetime.

datetime.strftime()方法用于操作模块datetime的datetime类的对象。

It takes an instance of the class and returns a string representing the date and time, which is controlled by an explicit format string.

它使用该类的实例,并返回表示日期和时间的字符串,该字符串由显式格式字符串控制。

Module:

模块:

    import datetime

Class:

类:

    from datetime import datetime

Syntax:

句法:

    strftime(format)

Parameter(s):

参数:

  • format - it is the string format code based on which string representation and formatting occurs. For example, %Y, %m, %d, etc. are format codes. This method takes one or more format codes as an argument and returns a formatted string based on that.

    format-它是字符串格式代码,基于该代码的字符串表示形式和格式发生。 例如, %Y , %m , %d等是格式代码。 此方法将一个或多个格式代码作为参数,并根据该格式返回格式化的字符串。

Given below is a list of all the format codes available:

以下是所有可用格式代码的列表:

DirectiveMeaningExample
%aAbbreviated weekday name.Sun, Mon, ...
%A Full weekday name.Sunday, Monday, ...
%wWeekday as a decimal number.0, 1, ..., 6
%dDay of the month as a zero-padded decimal.01, 02, ..., 31
%-dDay of the month as a decimal number.1, 2, ..., 30
%bAbbreviated month name.Jan, Feb, ..., Dec
%BFull month name.January, February, ...
%mMonth as a zero-padded decimal number.01, 02, ..., 12
%-mMonth as a decimal number.1, 2, ..., 12
%yYear without century as a zero-padded decimal number.00, 01, ..., 99
%-yYear without century as a decimal number.0,1, 2, … , 99
%YYear with century as a decimal number.2020, 2019 etc.
%HHour (24-hour clock) as a zero-padded decimal number.00, 01, ..., 23
%-HHour (24-hour clock) as a decimal number.0, 1, 2, ..., 23
%IHour (12-hour clock) as a zero-padded decimal number.01, 02, ..., 12
%-IHour (12-hour clock) as a decimal number.1,2,.., 12
%pLocal AM or PM.AM, PM
%MMinute as a zero-padded decimal number.00, 01, ..., 59
%-MMinute as a decimal number.0, 1, ..., 59
%SSecond as a zero-padded decimal number.00, 01, ..., 59
%-SSecond as a decimal number.0, 1, ..., 59
%fMicrosecond as a decimal number,zero-padded on the left.000000 - 999999
%zUTC offset in the form +HHMM or -HHMM.N/A
%ZTime zone name.N/A
%jDay of the year as a zero-padded decimal number.001, 002, ..., 366
%-jDay of the year as a decimal number.1, 2, ..., 366
%cAppropriate local date and time representationWed Oct 27 01:04:15 2020
%x Appropriate local date representation.09/30/13
%XAppropriate local time representation.09:07:06
%UWeek number of the year, with Sunday as the first day of the week00, 01, ..., 53
%WWeek number of the year, with Monday as the first day of the week00, 01, ..., 53
指示 含义
%一个 工作日名称的缩写。 周日,周一...
%一个 工作日全名。 星期天星期一, ...
%w 工作日为十进制数字。 0,1,...,6
%d 月份中的一天,以零填充的十进制表示。 01,02,...,31
%d 以十进制数表示的月份中的一天。 1,2,...,30
%b 缩写的月份名称。 一月,二月,...,十二月
%B 完整的月份名称。 一月二月, ...
%m 以零填充的十进制数字表示的月份。 01、02,...,12
%-m 以十进制数表示的月份。 1,2,...,12
%y 无世纪的年份,为零填充的十进制数字。 00、01,...,99
%-y 没有世纪的年份作为十进制数字。 0,1,2,…,99
%Y 以世纪作为十进制数字的年份。 2020、2019等
%H 小时(24小时制),为补零的十进制数字。 00、01,...,23
%-H 小时(24小时制)为十进制数字。 0,1,2,...,23
%一世 小时(12小时制),为零填充的十进制数字。 01、02,...,12
%-一世 小时(12小时制)为十进制数字。 1,2,..,12
%p 本地AM或PM。 上午下午
%M 分钟,为零填充的十进制数字。 00、01,...,59
%-M 以十进制数字表示。 0,1,...,59
%S 第二个为零填充的十进制数。 00、01,...,59
%-S 第二个十进制数字。 0,1,...,59
%F 微秒,十进制数,在左侧补零。 000000-999999
%z UTC偏移量,格式为+ HHMM或-HHMM。 不适用
%Z 时区名称。 不适用
%j 一年中的一天,为零填充的十进制数字。 001,002,...,366
%-j 一年中的天,以十进制数字表示。 1,2,...,366
%C 适当的本地日期和时间表示 2020年10月27日星期三01:04:15
%X 适当的本地日期表示形式。 13/9/30
%X 适当的本地时间表示。 09:07:06
%U 一年中的第几周,以星期日为一周的第一天 00、01,...,53
%W 一年中的第几周,星期一为一周的第一天 00、01,...,53

Return value:

返回值:

The return type of this method is a string converted according to the format code.

此方法的返回类型是根据格式代码转换的字符串。

Example:

例:

## Python program explaining the 
## use of strftime() method in datetime class

from datetime import datetime
import pytz

## Creating an instance
x = datetime.now()
timezone = pytz.timezone("Asia/Kolkata")
## Adding timezone information in datetime object
x = x.astimezone(timezone)
c = x.strftime("%c")
print("Date and time representation using strftime:", c)
print()

##You can extract information using these methods
print("Abbreviated weekday name:", x.strftime("%a"))
print("Full weekday name:", x.strftime("%A"))
print("Weekday as a decimal number:", x.strftime("%w"))
print("Day of the month as a zero-padded decimal:", x.strftime("%d"))
print("Full month name:", x.strftime("%B"))
print("Month as a decimal number:", x.strftime("%-m"))
print("Year with century as a decimal number:", x.strftime("%Y"))
print("What day of the year is it?", x.strftime("%-j"))
print("What is the week number of this day?", x.strftime("%U"))##or x.strftime("%W")
print("What is the name of the timezone given", x.strftime("%Z"))
print("Is it AM or PM", x.strftime("%p"))
print("Time ahead of UTC time by:", x.strftime("%z"))
print()

## Different ways of representing the Date as a date string
print("Output 1: date:", x.strftime("%d/%m/%Y"))
print("Output 2: time:", x.strftime("%H:%M:%S:%f"))
print("Output 3: time with timezone:", x.strftime("%H:%M:%S:%f %z"))
print("Output 4: date with day: ", x.strftime("%a %d/%B/%Y"))
print("Output 5:", x.strftime("%A, %Y/%m/%d, %H:%M:%S:%f %Z"))
print("Output 6:", x.strftime("%x"))
print("Output 7:", x.strftime("%X"))
## You can create any combination of the date and time 
## representation you want 

Output

输出量

Date and time representation using strftime: Sun May  3 23:08:28 2020

Abbreviated weekday name: Sun
Full weekday name: Sunday
Weekday as a decimal number: 0
Day of the month as a zero-padded decimal: 03
Full month name: May
Month as a decimal number: 5
Year with century as a decimal number: 2020
What day of the year is it? 124
What is the week number of this day? 18
What is the name of the timezone given IST
Is it AM or PM PM
Time ahead of UTC time by: +0530

Output 1: date: 03/05/2020
Output 2: time: 23:08:28:024355
Output 3: time with timezone: 23:08:28:024355 +0530
Output 4: date with day:  Sun 03/May/2020
Output 5: Sunday, 2020/05/03, 23:08:28:024355 IST
Output 6: 05/03/20
Output 7: 23:08:28


翻译自: https://www.includehelp.com/python/datetime-strftime-method-with-example.aspx

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中,datetime.strftime()函数用于将日期时间对象转换为指定格式的字符串。通过指定格式化字符串,我们可以以不同的方式显示日期和时间的各个部分。引用中的示例展示了如何使用strftime()函数来格式化年份,示例代码如下: ```python from datetime import datetime def main(): now = datetime.now() formatted_date = now.strftime("%Y") print(formatted_date) main() ``` 这段代码将当前日期时间对象的年份转换为字符串并打印出来。在strftime()函数中,"%Y"代表年份的格式化指令。 除了年份,我们还可以使用其他格式化指令来格式化日期和时间的其他部分。例如,在引用中的示例中,我们可以使用"%I:%M:%S %p"来格式化时间为12小时制,并带有上午/下午的标识,使用"%H:%M"来格式化时间为24小时制,示例代码如下: ```python from datetime import datetime def main(): now = datetime.now() formatted_time_with_ampm = now.strftime("%I:%M:%S %p") formatted_time_24hr = now.strftime("%H:%M") print(formatted_time_with_ampm) print(formatted_time_24hr) main() ``` 这段代码将当前日期时间对象的时间部分转换为12小时制的字符串和24小时制的字符串并打印出来。"%I:%M:%S %p"将时间格式化为12小时制,并带有上午/下午的标识,"%H:%M"将时间格式化为24小时制。 综上所述,datetime.strftime()函数可以将日期时间对象格式化为指定格式的字符串,并且我们可以使用不同的格式化指令来显示日期和时间的各个部分。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Python学习之DateTime、TimeDelta、Strftime(Format)及其示例](https://blog.csdn.net/u014740628/article/details/130267785)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值