1. 程式人生 > >Python日期時間型別轉換

Python日期時間型別轉換

一、time轉換為datetime

1)datetime.datetime.fromtimestamp(tm )

2)datetime.datetime.utcfromtimestamp(tm )

二、datetime->time

1)dt01.timestamp()

2)dt01.timetuple()

演示:

from time importtime

from datetime importdatetime

time01 = time()

print("轉換前:", time01)

print("轉換後:", datetime.fromtimestamp(time01))

dt01 = datetime.today()

print("轉換前:", dt01)

print("轉換成時間戳:", dt01.timestamp())

print("轉換成時間的元組:", dt01.timestamp())

執行結果:

C:\python\python.exeC:/python/demo/file3.py

轉換前:1519892355.5060892

轉換後:2018-03-01 16:19:15.506089

轉換前:2018-03-01 16:19:15.506089

轉換成時間戳:1519892355.506089

轉換成時間的元組:1519892355.506089

Process finished with exit code 0