欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

时间戳,时间元组,字符串三者的转换

程序员文章站 2022-05-05 15:05:31
...

1.时间戳------->时间元组:

time1 = time.time()
tuple = time.gmtime(time1)  # UTC时间
print(tuple)
tuple1 = time.localtime(time1)  # UTC + 8 时间
print(tuple1)
2.时间元组-------->时间戳:

tuple2 = time.localtime()
time2 = time.mktime(tuple2)
print(time2)
3.时间元组--------->字符串:

tuple = time.localtime()
strTime = time.strftime("%Y-%m-%d %H:%M:%S",tuple)
print(strTime)
strTime1 = time.strftime("%Y{y}%m{m}%d{d} %H{h}%M{m1}%S{s}",tuple).format(y="年",m="月",d="日",h="时",m1="分",s="秒")
4.字符串---------->时间元组:

tupleTim e = time.strptime("2018年01月05日","%Y{y}%m{m}%d{d}".format(y="年",m = "月",d="日"))
print(tupleTime)
欢迎补充和纠错,谢谢!