import datetime
import time
# 时间戳转换为datetime类型 1653398780 ---> class 'datetime.datetime'
def timestamp_to_datetime(time_stamp=time.time()):
datetime_obj = datetime.datetime.fromtimestamp(time_stamp)
return datetime_obj
# 时间字符串转换为时间戳 "2022-05-24 21:26:20" ---> 1653398780
def timestr_to_timestamp(time_str):
timetamp = int(time.mktime(time.strptime(time_str, "%Y-%m-%d %H:%M:%S")))
return timetamp
if __name__ == '__main__':
print(time.time())
ret1 = timestamp_to_datetime()
print(ret1, type(ret1))
ret2 = timestr_to_timestamp("2022-05-24 21:26:20")
print(ret2)
12-08
120