例如,在2010年7月5日,我想计算字符串
July 5, 2010
Run Code Online (Sandbox Code Playgroud)
该怎么做?
Dav*_*ebb 205
您可以使用该datetime
模块在Python中处理日期和时间.该strftime
方法允许您使用指定的格式生成日期和时间的字符串表示形式.
>>> import datetime
>>> datetime.date.today().strftime("%B %d, %Y")
'July 23, 2010'
>>> datetime.datetime.now().strftime("%I:%M%p on %B %d, %Y")
'10:36AM on July 23, 2010'
Run Code Online (Sandbox Code Playgroud)
Pie*_*ter 27
#python3
import datetime
print(
'1: test-{date:%Y-%m-%d_%H:%M:%S}.txt'.format( date=datetime.datetime.now() )
)
d = datetime.datetime.now()
print( "2a: {:%B %d, %Y}".format(d))
# see the f" to tell python this is a f string, no format
print(f"2b: {d:%B %d, %Y}")
print(f"3: Today is {datetime.datetime.now():%Y-%m-%d} yay")
Run Code Online (Sandbox Code Playgroud)
1:test-2018-02-14_16:40:52.txt
2:2018年3月4日
3:今天是2018-11-11 yay
描述:
使用新的字符串格式将值注入占位符{}的字符串中,value是当前时间.
然后,而不是仅将原始值显示为{},使用格式来获取正确的日期格式.
https://docs.python.org/3/library/string.html#formatexamples
Pär*_*der 23
>>> import datetime
>>> now = datetime.datetime.now()
>>> now.strftime("%B %d, %Y")
'July 23, 2010'
Run Code Online (Sandbox Code Playgroud)
如果你不关心格式,你只需要一些快速的日期,你可以使用这个:
import time
print(time.ctime())
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
154577 次 |
最近记录: |