直接上python3代码

# 对时间类型的转换
from datetime import datetime 
import locale

locale.setlocale(locale.LC_CTYPE, "chinese")

# 字符串 -> datetime类型
text = "2024年-4月-1日"
res = datetime.strptime(text, "%Y年-%m月-%d日")
print("datetime类型: ", res)

# datetime类型 -> 字符串
date = datetime.now().strftime("%Y年-%m月-%d日")
print("字符串:" + date)

# datetime类型 -> 时间戳
v1 = datetime.now()
res1 = v1.timestamp()
print("时间戳:", res1)

# 时间戳 -> datetime类型
import time 
v2 = time.time()
res2 = datetime.fromtimestamp(v2)
print("datetime: " , res2)

# 结果如下
datetime类型:  2024-04-01 00:00:00
字符串:2024-04-19日
时间戳: 1713505850.690898
datetime:  2024-04-19 13:50:50.690898


利用这个写一个例子

from datetime import datetime 
import time 

startTime = "2024-04-01 1:00"
startTimeDate = datetime.strptime(startTime, "%Y-%m-%d %H:%M")

endTime = "2024-04-01 10:00"
endTimeDate = datetime.strptime(endTime, "%Y-%m-%d %H:%M")

v1 = endTimeDate - startTimeDate
print(v1)

# 结果如下
9:00:00


点个赞呗~

04-19 19:32