Python当前时间的几种表示方式
#!/usr/bin/env python# -*- coding: utf-8 -*-import timet1 = time.time()# 当前时间戳print('t1:', t1)t2 = time.ctime()print('t2:', t2)t3 = time.localtime()# 时间元组,默认参数为当前时间戳print('t3:', t3)t4 = ...
·
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
t1 = time.time() # 当前时间戳
print('t1:', t1)
t2 = time.ctime()
print('t2:', t2)
t3 = time.localtime() # 时间元组,默认参数为当前时间戳
print('t3:', t3)
t4 = "%s-%s-%s" % (t3.tm_year, t3.tm_mon, t3.tm_mday)
print('t4:', t4)
t5 = time.strftime("%X", time.localtime())
print('t5:', t5)
t6 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
print('t6:', t6)
from datetime import datetime
t7 = datetime.now()
print('t7:', t7)
t8 = format(t7, '%Y-%m-%d %H:%M:%S')
print('t8:', t8)
打印结果:
t1: 1565747398.8793676
t2: Wed Aug 14 09:49:58 2019
t3: time.struct_time(tm_year=2019, tm_mon=8, tm_mday=14, tm_hour=9, tm_min=49, tm_sec=58, tm_wday=2, tm_yday=226, tm_isdst=0)
t4: 2019-8-14
t5: 09:49:58
t6: 2019-08-14 09:49:58
t7: 2019-08-14 09:49:58.968390
t8: 2019-08-14 09:49:58
参考资料:https://www.runoob.com/python/python-date-time.html
内置的 format() 函数和 str.format() 方法的几个示例:
>>> from datetime import datetime
>>> now = datetime.now()
>>> format(now, '%H:%M:%S')
'18:49:05'
>>> "It's now {:%I:%M %p}".format(now)
"It's now 06:49 PM"
更多推荐
已为社区贡献12条内容
所有评论(0)