Converting Datetime Objects to Epoch Time (Unix Time) in Python
In Python, converting datetime objects to Unix time, or milliseconds since the 1970 epoch, is a common task. The following question delves into how to accomplish this conversion effectively:
Question:
How can I convert a Python datetime object to unix time, or seconds/milliseconds since the 1970 epoch?
Answer:
One of the most straightforward solutions is to utilize the "unix_time_millis" function, as demonstrated below:
import datetime epoch = datetime.datetime.utcfromtimestamp(0) def unix_time_millis(dt): return (dt - epoch).total_seconds() * 1000.0
Explanation:
By using this function, you can effortlessly convert any datetime object to Unix time in milliseconds.
以上是如何將 Python 日期時間物件轉換為 Unix 時間(自 1970 紀元以來的秒/毫秒)?的詳細內容。更多資訊請關注PHP中文網其他相關文章!