Python strftime的定义
在这篇文章中,我们将讨论Python中的strftime()函数。这个函数同时存在于datetime和time模块中,strftime()函数的工作原理在两个模块中都是一样的,只是语法不同。在这篇文章中,我们将看到strftime()是一个用来将localtime()或gmtime()返回的日期和时间格式转换成指定格式的可读字符串的函数。datetime模块是python中的一个内置模块。strftime()函数以格式为参数,在Python中指定了不同的格式,根据参数中指定的格式返回可读字符串中的日期和时间。
strftime()函数在Python中的作用
本节将看到strftime()函数,它将时间对象转换为指定格式的可读字符串。这个函数在Python的datetime模块和time模块中都可用。让我们看看strftime()在这两个模块中的语法和例子。
datetime模块中的语法
do.strftime(format)
参数
- **format:**用于指定字符串格式,这意味着它有一个指令列表,可用于指定格式字符串。
在时间模块中的语法
t.strftime(format [,t])
参数
- **t:**用于指定以秒为单位的时间。
- format:用于指定字符串的格式,这意味着它有一个可以用来指定格式字符串的指令列表。
让我们看看指令或格式代码的列表。
- 要以简短的形式表示工作日的名称,那么我们使用"%a"。
- 要以完整的形式表示工作日的名称,那么我们使用"%A"。
- 要以短语形式表示月份的名称,我们使用"%b"。
- 要以完整的形式表示月份的名称,我们使用"%B"。
- 用十进制数字表示年份,不含世纪,我们使用"%-y"。
- 要用十进制数字表示年份,要用"%Y"。
- 以小数点后的24小时格式表示时间,我们使用"%-H"。
- 以小数点形式表示12小时的时间,则使用"%-I"。
- 用小数点后的数字表示秒,我们使用"%-S"。
- 为了表示时区的名称,我们使用"%Z"。
有许多这样的指令用于指定格式代码,以将日期-时间格式转换成可读的字符串格式。
Python中strftime()函数的例子
现在让我们看看下面的例子,使用上述格式指令代码将给定的时间和时间格式转换为可读的字符串。我们将同时使用数据时间模块和时间模块的例子。我们应该注意,这个时间戳的范围是0到60。但在少数情况下,这意味着由于一些历史原因,时间戳的范围是从0到61。
例子 #1
让我们看一个例子来演示strftime()函数在Python中使用datetime模块。
代码
from datetime import datetime as dt print("Python program to demonstrate strftime() function using datetime module") print("\n") now = dt.now() print("The current date and time without formatting is as follows:") print(now) print("\n") s = now.strftime("%a %m %y") print("This will display shortform of weekday, month, year without century:") print(s) print("\n") s = now.strftime("%A %-m %Y") print("This will display fullform of weekday name, month name without zero,year with cetury: ") print(s) print("\n") s = now.strftime("%-I %p %S") print("This will display the time in 12-h format, prime meridiean, seconds:") print(s) print("\n") s = now.strftime("%-j") print("This will display the day of the year in decimal number:") print(s)
输出
在上面的程序中,我们可以看到我们使用了datetime模块,然后我们创建了日期时间对象 "dt",然后我们可以调用strftime()函数。但是要打印当前的日期和时间而不使用strftime(),那么我们就使用now()函数;随后,我们使用上面给出的各种格式代码将这种格式转换成可读的字符串。
例子 #2
让我们看一个使用时间模块演示strftime()函数的例子。
代码
from time import gmtime, strftime print("Program to demonstrate strftime() function using time module") print("\n") s = strftime("%a, %d %b %Y %H:%M:%S + 1010", gmtime()) print("This will display the simple format of date and time using format codes with short forms") print(s) print("\n") s = strftime("%A, %D %B %Y %H:%M:%S + 0000", gmtime()) print("This will display the simple format of date and time using format codes with full forms") print(s) print("\n") # this will show you the preferd date time format s = strftime("%c") print("This will display prefered date and time format") print(s) print("\n") s = strftime("%C") print("This will dispay the century in decimal number") print(s) print("\n") s = strftime("%r, %z, %Z") print("This will dispay the name of the time zones with time") print(s)
输出
在上面的程序中,我们可以看到我们有一个导入的时间模块,我们正在使用strftime()函数和上面给出的列表中的格式代码。
总结
在这篇文章中,我们总结了使用strftime()函数;我们需要使用时间模块或数据时间模块。在这篇文章中,我们看到strftime()函数主要用于使用指定的格式代码或指令将日期和时间从与时间相关的对象显示为可读字符串。在这篇文章中,我们还列出了一些指令或格式代码及其含义。