Please help me to change datetime object (for example: 2011-12-17 11:31:00-05:00
) (including timezone) to Unix timestamp (like function time.time() in Python).
In the time module, the timegm function returns a Unix timestamp. The timetuple() function of the datetime class returns the datetime's properties as a named tuple. To obtain the Unix timestamp, use print(UTC).
Use datetime. fromtimestamp() of the datetime module to convert Unix time (Epoch time) to datetime object. Specify Unix time as an argument. By default, it is converted to the local date and time.
Another way is:
import calendar from datetime import datetime d = datetime.utcnow() timestamp=calendar.timegm(d.utctimetuple())
Timestamp is the unix timestamp which shows the same date with datetime object d.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With