1.字符串格式的时间转时间戳 import time a = "2022-04-19 08:51:31" #先转换成时间数组 time_array = time.strptime(a,'%Y-%m-%d %H:%M:%S') print(time_array) #时间数组转时间戳 time_stamp =int(time.mktime(time.strptime(a
1.字符串格式的时间转时间戳
import timea = "2022-04-19 08:51:31"
#先转换成时间数组
time_array = time.strptime(a,'%Y-%m-%d %H:%M:%S')
print(time_array)
#时间数组转时间戳
time_stamp =int(time.mktime(time.strptime(a,'%Y-%m-%d %H:%M:%S')))
print(time_stamp)
结果:
time.struct_time(tm_year=2022, tm_mon=4, tm_mday=19, tm_hour=8, tm_min=51, tm_sec=31, tm_wday=1, tm_yday=109, tm_isdst=-1)
1650329491
2.字符串时间戳转时间
import timetime_stamp = 1650329491
#转时间数组
time_array = time.localtime(time_stamp)
print(time_array)
#转时间
time_time = time.strftime("%Y-%m-%d %H:%M:%S", time_array)
print(time_time)
结果:
time.struct_time(tm_year=2022, tm_mon=4, tm_mday=19, tm_hour=8, tm_min=51, tm_sec=31, tm_wday=1, tm_yday=109, tm_isdst=0)
2022-04-19 08:51:31
3.常用的时间练习
import timeclass Time():
def now_time(self):
return time.strftime('%H:%M:%S', time.localtime(time.time()))
def now_date(self):
return time.strftime('%Y-%m-%d', time.localtime(time.time()))
def now_date_time(self):
return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
def now_time_stamp_10(self):
return int(time.time())
def now_time_stamp_13(self):
return int(time.time()*1000)
if __name__ == '__main__':
ss=Time()
print(ss.now_time())
print(ss.now_date())
print(ss.now_date_time())
print(ss.now_time_stamp_10())
print(ss.now_time_stamp_13())
搜索
复制