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

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

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

It takes an instance of the class and returns a string representing the date, which is controlled by an explicit format string. Since this class has attributes related to dates only, format codes referring to hours, minutes, or seconds have 0 values.

它使用类的实例,并返回表示日期的字符串,该字符串由显式格式字符串控制。 由于此类仅具有与日期有关的属性,因此引用小时,分钟或秒的格式代码具有0值。

Module:

模块:

    import datetime

Class:

类:

    from datetime import date

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:

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

Directive Meaning Example
%a Abbreviated weekday name. Sun, Mon, ...
%A Full weekday name. Sunday, Monday, ...
%w Weekday as a decimal number. 0, 1, ..., 6
%d Day of the month as a zero-padded decimal. 01, 02, ..., 31
%-d Day of the month as a decimal number. 1, 2, ..., 30
%b Abbreviated month name. Jan, Feb, ..., Dec
%B Full month name. January, February, ...
%m Month as a zero-padded decimal number. 01, 02, ..., 12
%-m Month as a decimal number. 1, 2, ..., 12
%y Year without century as a zero-padded decimal number. 00, 01, ..., 99
%-y Year without century as a decimal number. 0,1, 2, … , 99
%Y Year with century as a decimal number. 2020, 2019 etc
%H Hour (24-hour clock) as a zero-padded decimal number. 00, 01, ..., 23
%-H Hour (24-hour clock) as a decimal number. 0, 1, 2, ..., 23
%I Hour (12-hour clock) as a zero-padded decimal number. 01, 02, ..., 12
%-I Hour (12-hour clock) as a decimal number. 1,2,.., 12
%p Local AM or PM. AM, PM
%M Minute as a zero-padded decimal number. 00, 01, ..., 59
%-M Minute as a decimal number. 0, 1, ..., 59
%S Second as a zero-padded decimal number. 00, 01, ..., 59
%-S Second as a decimal number. 0, 1, ..., 59
%f Microsecond as a decimal number,zero-padded on the left. 000000 - 999999
%z UTC offset in the form +HHMM or -HHMM.
%Z Time zone name.
%j Day of the year as a zero-padded decimal number. 001, 002, ..., 366
%-j Day of the year as a decimal number. 1, 2, ..., 366
%c Appropriate local date and time representation Wed Oct 27 01:04:15 2020
%x Appropriate local date representation. 09/30/13
%X Appropriate local time representation. 09:07:06
%U Week number of the year, with Sunday as the first day of the week 00, 01, ..., 53
%W Week number of the year, with Monday as the first day of the week 00, 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

Please note all the codes which have time and timezone information, will have 0 values as date class only contains date attributes.

请注意,所有具有时间和时区信息的代码都将具有0值,因为日期类仅包含日期属性。

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 date class
from datetime import date
## Creating an instance
x = date.today()
c = x.strftime("%c")
print("Date 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()
## Different ways of representing the Date
## as a date string
print("Output 1:", x.strftime("%d %m %Y"))
print("Output 2:", x.strftime("%-d %-m %y, %H %M %S"))
## time values will be 0
print("Output 3:", x.strftime("%a %d %B %Y"))
print("Output 4:", x.strftime("%A, %Y %m %d, %H:%M:%S"))
print("Output 5:", x.strftime("%x"))
## You can create any combination of the date
## representation you want

Output

输出量

Date representation using strftime: Thu Apr 30 00:00:00 2020
Abbreviated weekday name: Thu
Full weekday name: Thursday
Weekday as a decimal number: 4
Day of the month as a zero-padded decimal: 30
Full month name: April
Month as a decimal number: 4
Year with century as a decimal number: 2020
What day of the year is it? 121
What is the week number of this day? 17
Output 1: 30 04 2020
Output 2: 30 4 20, 00 00 00
Output 3: Thu 30 April 2020
Output 4: Thursday, 2020 04 30, 00:00:00
Output 5: 04/30/20

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

带有示例的Python date strftime()方法相关推荐

  1. python 示例_带有示例的Python date timetuple()方法

    python 示例 Python date.timetuple()方法 (Python date.timetuple() Method) date.timetuple() method is used ...

  2. python 示例_带有示例的Python date isocalendar()方法

    python 示例 Python date.isocalendar()方法 (Python date.isocalendar() Method) date.isocalendar() method i ...

  3. 带有示例的Python date weekday()方法

    Python date.weekday()方法 (Python date.weekday() Method) date.weekday() method is used to manipulate o ...

  4. lock_sh 示例_带有示例的Python date __str __()方法

    lock_sh 示例 Python date .__ str __()方法 (Python date.__str__() Method) date.__str__() method is used t ...

  5. 带有示例的Python datetime weekday()方法

    Python datetime.weekday()方法 (Python datetime.weekday() Method) datetime.weekday() method is used to ...

  6. python wait之后怎么起起来_python wait方法_Python条件类| 带有示例的wait()方法

    python wait方法 Python Condition.wait()方法 (Python Condition.wait() Method) wait() is an inbuilt method ...

  7. flush python_带有示例的Python File flush()方法

    flush python 文件flush()方法 (File flush() Method) flush() method is an inbuilt method in Python, it is ...

  8. python wait方法_Python条件类| 带有示例的wait()方法

    python wait方法 Python Condition.wait()方法 (Python Condition.wait() Method) wait() is an inbuilt method ...

  9. python线程任务run_Python线程类| 带有示例的run()方法

    python线程任务run Python Thread.run()方法 (Python Thread.run() Method) Thread.run() method is an inbuilt m ...

最新文章

  1. 使用OpenVINO加速Pytorch表情识别模型
  2. 第二届中国云计算与SaaS学术会议征文通知
  3. 自然语言处理之词向量模型(三)
  4. selinux禁用后系统无法正常启动的问题
  5. mongoDB 删除集合后,空间不释放
  6. 【cocos2d-x从c++到js】06:Google的继承写法解析
  7. UI5 setBusyIndicatorDelay
  8. FFT/NTT卷积神级副本
  9. publiccms中将推荐页的内容显示在页面片段中
  10. 一般微型计算机有几十条,计算机单选题.doc
  11. python让你再也不为文章配图与素材发愁,让高清图片占满你的硬盘! #华为云·寻找黑马程序员#
  12. LeetCode-114: 二叉树展开为链表
  13. python内置类属性_python常用内建属性大全
  14. Sql Server 2000 无法打开用户默认数据库。登录失败
  15. bootstrap-内联文本元素-斜体
  16. IIS 7.5学习笔记(二)IIS简史:从IIS 1.0到IIS 7.5
  17. 【图像超分辨(SR)】通俗直观彻底理解双线性插值、双三次插值及其作用
  18. shell脚本 插队
  19. python的合法命名,以下不是Python语言合法命名的是:A、MyGod5B、5MyGodC、_...
  20. 全加器和半加器的区别

热门文章

  1. php页面的循环输出数组,PHP抓取页面上的数组 并循环输出 急
  2. nvidia-smi 命令详解
  3. 飞行计算机人机工程,人机工程学版
  4. python gui入门的例子_Python GUI编程之Tkinter入门之道
  5. 存储过程与函数oracle
  6. 【iCore4 双核心板_FPGA】例程十六:基于双口RAM的ARM+FPGA数据存取实验
  7. 使用大数据闪存打造融合数据平台
  8. TCP/IP协议网络模型
  9. UIScrollView的简单使用
  10. 记一则js替换字符串的问题