python中日期格式和时间戳格式的转换
import timefrom datetime import date# 时间转换成时间戳d = date(2022, 3, 18)timeArray = time.strptime(str(d), "%Y-%m-%d")print(timeArray)print(timeArray.tm_year)ret = int(time.mktime(timeArray))print(ret)# 时间戳
·
方法一:
import time
from datetime import date
# 时间转换成时间戳
d = date(2022, 3, 18)
timeArray = time.strptime(str(d), "%Y-%m-%d")
print(timeArray)
print(timeArray.tm_year)
ret = int(time.mktime(timeArray))
print(ret)
# 时间戳转换成时间
ret2 = time.localtime(ret)
print(ret2)
ret3 = time.strftime("%Y-%m-%d", ret2)
print(ret3, type(ret3))
方法二:
import time
from datetime import date
if user.birthday:
# 日期格式转换成时间戳 user.birthday: date类型
u_time = int(time.mktime(user.birthday.timetuple()))
print(u_time)
# 时间戳转换成日期
d_time = date.fromtimestamp(u_time)
print(d_time)
更多推荐
已为社区贡献6条内容
所有评论(0)