gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
python datetime to unix seconds from 1970
- User
python datetime to unix seconds from 1970
- Assistant
To convert a Python
datetimeobject to Unix epoch time (the number of seconds since January 1, 1970), you can use thetimestamp()method available ondatetimeobjects. Here’s how to do it:- Import the
datetimemodule. - Create a
datetimeobject. - Call the
timestamp()method to get the Unix time.
Here’s an example:
from datetime import datetime # Create a datetime object (Example: current date and time) dt = datetime.now() # Convert to Unix timestamp unix_timestamp = dt.timestamp() print("Datetime:", dt) print("Unix Timestamp:", unix_timestamp) - Import the