將日期時間轉換爲 Unix 時間戳並將其轉換回 python - Convert datetime to Unix timestamp and convert it back in python

問題:

I have dt = datetime(2013,9,1,11) , and I would like to get a Unix timestamp of this datetime object.我有dt = datetime(2013,9,1,11) ,我想獲得這個 datetime 對象的 Unix 時間戳。

When I do (dt - datetime(1970,1,1)).total_seconds() I got the timestamp 1378033200 .當我這樣做時(dt - datetime(1970,1,1)).total_seconds()我得到了時間戳1378033200

When converting it back using datetime.fromtimestamp I got datetime.datetime(2013, 9, 1, 6, 0) .使用datetime.fromtimestamp將其轉換回來時,我得到了datetime.datetime(2013, 9, 1, 6, 0)

The hour doesn't match.小時不匹配。 What did I miss here?我在這裏錯過了什麼?


解決方案:

參考一: https://stackoom.com/question/1L5Kh
參考二: Convert datetime to Unix timestamp and convert it back in python
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章