时间戳和格式化时间的转化
看见别人搞的时间戳、格式化时间的转化,咱也搞了个
1、格式化时间转换时间戳
import time
def str_to_stamp(str=None,format='%Y-%m-%d %H:%M:%S'):
# 格式化好的时间转时间戳的,如果不传格式化好的时间,就返回当前的时间戳
if str:
return int(time.mktime(time.strptime(str,format)))
else:
return int(time.time())
print(str_to_stamp())
print(str_to_stamp('2019-12-13 22:23:24'))
print(str_to_stamp('2019-12-13','%Y-%m-%d'))
1575516985
1576247004
1576166400
2、时间戳转换格式化时间
import time
def stamp_to_str(stamp=None,format='%Y-%m-%d %H:%M:%S'):
'''这个是把时间戳转换成格式化好的实际,如果不传时间戳,那么就返回当前的时间'''
if stamp:
return time.strftime(format,time.localtime(stamp))
else:
return time.strftime(format,time.localtime())
print(stamp_to_str(1575516985,'%Y-%m-%d'))
print(stamp_to_str(format='%Y-%m-%d'))
print(stamp_to_str())
2019-12-05
2019-12-05
2019-12-05 11:52:37

浙公网安备 33010602011771号