首页 > 编程知识 正文

Python 中的strftime()函数

时间:2023-11-25 15:30:21 阅读:309262 作者:STJI

在本教程中,我们将了解 Python 编程语言中str time()函数的工作原理及其变体。

所以,让我们开始吧。

正如我们已经知道的,Python 提供了各种模块,这些模块包含函数集群,以便在数据上实现不同类型的功能。利用 Python 的时间模块实现基于各种时间戳的操作。

此外,Python 的 strftime() 函数接受不同格式的时间,并返回一个以标准形式表示时间的字符串。

使用 Python strftime()函数获取当前时间

我们可以将 Pythonstr time()函数与 datetime 模块一起使用,以便按照格式代码以适当的格式获取当前时间戳。

其语法如下所示。

语法:


datetime.now().strftime('format codes')

这里,str time()函数的参数,格式代码实际上是为了以标准和适当的方式表示时间戳而使用的预定义代码。在本教程中,我们将更详细地了解格式代码。

但在此之前,让我们考虑一个例子。

示例:


# importing the datetime module
from datetime import datetime

# storing the value of current timestamp
cur_timestamp = datetime.now()

# using the strftime() function to represent
# timestamp into proper and standard format
the_time = cur_timestamp.strftime( "%H : %M : %S" )
the_date = cur_timestamp.strftime( "%d - %m - %Y" )

# printing the values
print("Present Time:", the_time)
print("Present Date:", the_date)

输出:

Present Time: 18 : 06 : 17
Present Date: 01 - 06 - 2021

说明:

在上例中,我们从日期时间库中导入了日期时间模块。然后,我们定义了一个存储当前日期和时间值的变量。然后,我们使用 strftime() 功能将当前日期和时间表示为指定的标准格式,并将其打印给用户。因此,当前日期和时间已成功地以适当的标准格式打印出来。

使用 Python strftime()函数和预定义的时间戳

有时,当我们想要显示过去数据集的日期和时间时,就会出现这种情况。借助 Python 中的str time()函数,我们可以执行同样的操作。

日期时间模块的frontimestamp()方法帮助用户获取预定义的时间戳。此外,我们可以使用 strftime() 函数,以便使用前面解释的不同格式代码以标准格式表示提取的时间戳。

我们可以看到str time()函数用法的语法如下:

语法:


datetime.fromtimestamp(timestamp).strftime('format code')

现在,让我们考虑一个示例,说明str time()函数的工作以及预定义的时间戳。

示例:


# importing the datetime module
from datetime import datetime

# provided timestamp
provided_timestamp = 114579923

# storing the value of provided timestamp
my_timestamp = datetime.fromtimestamp(provided_timestamp)

# using the strftime() function to represent
# timestamp into proper and standard format
the_time = my_timestamp.strftime( "%H : %M : %S" )
the_date = my_timestamp.strftime( "%d - %m - %Y" )

# printing the values
print("Time as per the provided Timestamp:", the_time)
print("Date as per the provided Timestamp:", the_date)

输出:

Time as per the provided Timestamp: 09 : 15 : 23
Date as per the provided Timestamp: 19 - 08 - 1973

说明:

在上面的例子中,我们已经定义了存储预定义时间戳值的变量。然后,我们使用frontime stamp()方法获取存储的值,并使用 strftime() 函数将获取的时间戳表示为适当的标准格式。然后,我们为用户打印了结果值。因此,时间戳已成功地以正确的指定格式表示。

使用 Python strftime()函数的各种格式代码

pythonstr time()函数利用不同的格式代码,以维护和标准的形式表示日期和时间。此外,这些格式代码可用于将小时、天、周等与时间戳分隔开来,并显示相同的内容。

让我们考虑一个表示格式代码用法的例子。

示例:


# importing the strftime function
from time import strftime

# using "%A" as the format code
# "%A" displays the current day of the local time
the_day = strftime( "%A" )

# printing the values
print("Present Day:", the_day)

输出:

Present Day: Tuesday

说明:

在上面的例子中,我们已经从时间模块导入了时间功能。然后,我们定义了一个变量,该变量使用 strftime() 函数以及参数“ %A ”作为当地时间当天的格式代码。然后,我们为用户打印了这些值。因此,程序相应地打印了当天的全名。

现在让我们考虑一些使用其他格式代码的例子。

1.使用“ %c ”作为格式码,以如下格式显示当前当地时间:

日月日期小时:分钟:秒年

示例:


# importing the strftime function
from time import strftime

# using "%c" as the format code
# "%c" displays the current local time
the_timestamp = strftime( "%c" )

# printing the values
print("Present Timestamp:", the_timestamp)

输出:

Present Timestamp: Wed Jun  2 16:06:41 2021

说明:

在上面的程序中,我们再次执行了与前面相同的程序。但是我们用了“ %c ”作为格式码。结果,程序以上面指定的格式代码打印了当前时间戳。

2.使用“ %R ”作为格式代码,以便以 24 小时格式表示时间。

示例:


# importing the strftime function
from time import strftime

# using "%R" as the format code
# "%R" displays the current time in 24-hour format
the_24hour = strftime( "%R" )

# printing the values
print("Present Time in 24-hour format:", the_24hour)

输出:

Present Time in 24-hour format: 16:14

说明:

我们在上面的程序中再次遵循了相同的语法,但是我们包含了格式代码“ %R ”以 24 小时格式显示当前时间。

3.使用“ %r ”作为格式代码,以 12 小时格式或小时:分:秒格式以及上午或下午符号表示时间。

示例:


# importing the strftime function
from time import strftime

# using "%r" as the format code
# "%r" displays the current local time in H:M:S format
the_time = strftime( "%r" )

# printing the values
print("Present Time in 12-hour format:", the_time)

输出:

Present Time in 12-hour format: 04:33:12 PM

说明:

与我们之前所做的类似,我们添加了“ %r ”作为str time()函数的参数,以 12 小时格式打印当前时间。

4.在一个函数中使用多个格式代码。

示例:


# importing the strftime function
from time import strftime

# using multiple format code
# "%x" displays the current date
# "%X" displays the current time in 24-hour format
# "%p" provides discriptions for current time i.e., AM or PM
the_timestamp = strftime( "%x -- %X %p" )

# printing the values
print("Present Timestamp:", the_timestamp)

输出:

Present Timestamp: 06/02/21 -- 16:44:11 PM

说明:

在上面的例子中,为了显示来自本地时间戳的日期,我们使用了格式代码“ %x ”。我们还使用了格式代码“ %X ”以 24 小时格式显示当前时间,同时使用了格式代码“ %p ”以提供当前时间的符号,即上午或下午。

除了上述格式代码,还有许多格式代码可用。下面列出了这些格式代码:

| 南号码 | 格式代码 | 描述 | | one | %a | 简而言之,工作日名称 | | Two | %A | 完整的工作日名称 | | three | %b | 简而言之,月份名称 | | four | %B | 完整的月份名称 | | five | %c | 选定的日期和时间表示 | | six | %C | 世纪数(年除以 100,范围从 00 到 99) | | seven | %d | 月日(从 01 到 31) | | eight | %D | 类似功能为 %m / %d / %y | | nine | %e | 月日(从 1 到 31) | | Ten | %g | 功能与 %G 类似;然而,价值是印刷没有世纪 | | Eleven | %G | 相当于 ISO 周数的 4 位数年份(参考 %V ) | | Twelve | %h | 功能与 %b 相似 | | Thirteen | %H | 小时,使用 24 小时制(从 00 到 23) | | Fourteen | %I | 小时,使用 12 小时制(从 01 到 12) | | Fifteen | %j | 年日(从 001 到 366) | | Sixteen | %m | 月份(从 01 到 12) | | Seventeen | %M | 分钟 | | Eighteen | %n | 换行符 | | Nineteen | %p | 根据提供的时间值,上午或下午 | | Twenty | %r | 以上午和下午符号表示的时间 | | Twenty-one | %R | 24 小时制时间 | | Twenty-two | %S | 第二 | | Twenty-three | %t | 制表符 | | Twenty-four | %T | 当前时间,等于“% H:% M:% S”格式 | | Twenty-five | %u | 以数字(范围从 1 到 7)表示的星期几,其中星期一被认为是 1。警告:在 Sun Solaris 中,星期日被视为 1。 | | Twenty-six | %U | 当前年份的周数,从第一个星期日作为第一周的第一天开始。 | | Twenty-seven | %V | 当前年份的 ISO 8601 周数(范围从 01 到 53),其中第 1 周是当前年份中至少有四天的第一周,星期一被视为第一周 | | Twenty-eight | %w | 以十进制表示的一周中的某一天,其中星期日被认为是 0。 | | Twenty-nine | %W | 当前年份的周数,从第一个星期一作为第一周的第一天开始。 | | Thirty | %x | 没有时间的日期选择描述 | | Thirty-one | %X | 没有日期的时间选择描述 | | Thirty-two | %y | 缺少一个世纪的年份(从 00 到 99) | | Thirty-three | %Y | 国际年与世纪 | | Thirty-four | %z 或%Z | 时区、名称或缩写 | | Thirty-five | %% | 实际的%字符 |


版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。