时间戳


#时间戳 从计算机诞生的那一天到现在过了多少秒
#格式化好的时间

import time

print(time.strftime('%Y%m%d%H%M%S'))#取当前的时间,格式化好的

print(time.time())
# print(1560674704 + 60*60*24*365*10)
#时间戳和格式化时间互相转换

# 时间元组,时间戳转成时间元组,
# time_tuple = time.gmtime(1560675002)#取标准时区的时间

#时间戳和格式化的好的时间互相转换
time_tuple = time.localtime(1568371007)#取本地时区的时间
print(time.strftime('%Y-%m-%d %H:%M:%S',time_tuple))

#格式化好的时间和时间戳互相转换

time_tuple = time.strptime('2019-09-13 18:36:47','%Y-%m-%d %H:%M:%S',)
#把格式化好的时间,转成时间元组
print(time.mktime(time_tuple))#把时间元组转成时间戳

def str_to_timestamp(str=None,format='%Y-%m-%d %H:%M:%S'):
#格式化好的时间转时间戳的,如果不传格式化好的时间,就返回当前的时间戳
if str:
time_tuple = time.strptime(str,format)
# 把格式化好的时间,转成时间元组
return int(time.mktime(time_tuple))
return int(time.time())

def timestamp_to_str(timestamp=None,format='%Y-%m-%d %H:%M:%S'):
'''这个是把时间戳转换成格式化好的实际,如果不传时间戳,那么就返回当前的时间'''
if timestamp:
time_tuple = time.localtime(timestamp) # 取本地时区的时间
return time.strftime(format, time_tuple)
return time.strftime(format)
原文地址:https://www.cnblogs.com/Dorami/p/11051396.html