Convert Datetime to Timestamp in Python

In Python, you can use the timestamp() method in the datetime module to convert a datetime object to a timestamp. Here is an example:

import datetime

# 创建一个datetime对象
dt = datetime.datetime(2021, 5, 1, 12, 0, 0)

# 转换为时间戳
timestamp = dt.timestamp()

print(timestamp)

The output result is:

1619851200.0

Please note that the timestamp() method returns a floating point number which represents the number of seconds elapsed since January 1, 1970, at midnight.

bannerAds