python strftime时分秒_Python time.strftime()用法及代碼示例

本文详细介绍了Python中的时间模块time.strftime()函数的使用,包括如何格式化时间,展示了多个示例,如基本时间格式、全名表示、日期时间格式、世纪、月份和日期等,并解释了各种指令在格式化字符串中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由於時間模塊提供了各種與時間有關的功能。因此,有必要導入時間模塊,否則將由於時間的定義而出錯。時間模塊中存在strftime(format [,t])。

time.strftime(format[, t])函數轉換代表時間的tuprl或struct_timegmtime()或者localtime()到由format參數指定的字符串。

如果未提供t,則返回當前時間localtime()用來。格式必須是字符串。如果t中的任何字段超出允許範圍,則引發ValueError。

注意:

0是時間元組中任何位置的合法論點;如果通常是非法的,則將該值強製為正確的值。

用法: time.srtftime(format[, t])

參數:

t-要格式化的時間(以秒為單位)

format-這是字符串類型。即指令可以嵌入格式字符串中。

返回值:沒有

格式字符串中可以嵌入許多指令,您可以在此處引用它們。

筆記:

當與strptime()函數一起使用時,僅當%I指令用於解析小時時,%p指令才會影響輸出小時字段。

範圍實際上是0到61。值60在表示leap秒的時間戳中有效,並且出於曆史原因支持值61。

與strptime()函數一起使用時,僅當指定星期幾和年份時,%U和%W僅用於計算。

下麵是實現:

# Program To show How can we use different derivatives

# Multiple at a time and single at a time

# importing the srtftime() and gmtime()

# if not used the gm time, time changes

# to the local time

from time import gmtime, strftime

# using simple format of showing time

s = strftime("%a, %d %b %Y %H:%M:%S + 1010", gmtime())

print("Example 1:", s)

print()

# only chnge in this is the full names

# and the representation

s = strftime("%A, %D %B %Y %H:%M:%S + 0000", gmtime())

print("Example 2:", s)

print()

# this will show you the preferd date time format

s = strftime("%c")

print("Example 3:", s)

print()

# this will tell about the centuries

s = strftime("%C")

print("Example 4:", s)

print()

# MOTY:month of the year

# DOTY:Day of the year

# Simple representation

# % n - new line

s = strftime("%A, %D %B %Y, %r, %nMOTY:%m %nDOTY:% j")

print("Example 5:", s)

print()

# % R - time in 24 hour notation

s = strftime(" %R ")

print("Example 6:", s)

print()

# % H - hour, using a 24-hour clock (00 to 23) in Example 1, 2, 3

# % I - hour, using a 12-hour clock (01 to 12)

s = strftime("%a, %d %b %Y %I:%M:%S + 0000", gmtime())

print("Example 7:", s)

print()

# % T - current time, equal to % H:% M:% S

s = strftime("%r, %T ", gmtime())

print("Example 8:", s)

print()

# % u an % U use (see difference)

s = strftime("%r, %u, %U")

print("Example 9:", s)

print()

# use of % V, % W, % w

s = strftime("%r, %V, %W, %w")

print("Example 10:", s)

print()

# use of % x, % X, % y, % Y

s = strftime("%x, %X, %y, %Y")

print("Example 11:", s)

print()

# use of % Z, % z

s = strftime("%r, %z, %Z")

print("Example 12:", s)

輸出:

Example 1:Tue, 25 Jun 2019 10:09:52 + 1010

Example 2:Tuesday, 06/25/19 June 2019 10:09:52 + 0000

Example 3:Tue Jun 25 10:09:52 2019

Example 4:20

Example 5:Tuesday, 06/25/19 June 2019, 10:09:52 AM,

MOTY:06

DOTY:% j

Example 6: 10:09

Example 7:Tue, 25 Jun 2019 10:09:52 + 0000

Example 8:10:09:52 AM, 10:09:52

Example 9:10:09:52 AM, 2, 25

Example 10:10:09:52 AM, 26, 25, 2

Example 11:06/25/19, 10:09:52, 19, 2019

Example 12:10:09:52 AM, +0000, UTC

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值