Skip to content

Instantly share code, notes, and snippets.

@Mulperi
Last active July 17, 2021 12:36
Show Gist options
  • Save Mulperi/4ad49e2d585ff0424998217b26ac51e5 to your computer and use it in GitHub Desktop.
Save Mulperi/4ad49e2d585ff0424998217b26ac51e5 to your computer and use it in GitHub Desktop.
Python datetime and timestamp
import datetime
# Create utc datetime object.
utcdatetime = datetime.datetime.utcnow()
print(utcdatetime)
# Create unix epoch timestamp from datetime.
milliseconds = int(datetime.datetime.timestamp(utcdatetime) * 1000)
timestamp = datetime.datetime.timestamp(utcdatetime)
print(milliseconds)
print(timestamp)
# Create datetime object from timestamp
datetime2 = datetime.datetime.fromtimestamp(timestamp)
print(datetime2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment