时间字符串转化为时间类型(两种方法)

时间:2025-05-07 07:53:14
《第一种》
import datetime
date_string = "2018-06-12 10"  #日 月 年
a = (date_string)
print("----",type(a),a)



《第二种》
from datetime import datetime
date_string = "2018-06-12 10"  #日 月 年
date_object = (date_string ,"%Y-%m-%d %H")

print("+++",a,type(a))

时间戳 转 字符串事件格式

import datetime

timestamp = 1699012435000 / 1000  # 将毫秒级的时间戳转换为秒级的时间戳

# 使用()方法将时间戳转换为时间类型
datetime_obj = (timestamp)
print(datetime_obj) # 打印:2023-11-03 19:53:55
# 可以根据需要将时间类型转换为不同的字符串格式
formatted_datetime = datetime_obj.strftime('%Y-%m-%d %H:%M:%S')
print(formatted_datetime) #打印:2023-11-03 19:53:55