Skip to content

Instantly share code, notes, and snippets.

@piyusht007
Last active September 21, 2020 10:10
Show Gist options
  • Save piyusht007/fbf083512e8706a6204e94fc21dceb42 to your computer and use it in GitHub Desktop.
Save piyusht007/fbf083512e8706a6204e94fc21dceb42 to your computer and use it in GitHub Desktop.
Date and time in Python
import datetime
from datetime import timezone
# Get timezone unaware current date time
print(datetime.datetime.now()) # 2020-09-18 22:17:52.878325
# Get timezone unaware current date time in UTC
print(datetime.datetime.utcnow()) # 2020-09-18 16:48:17.822981
# Preferred way is to use 'now' and pass it a timezone
print(datetime.datetime.now(timezone.utc)) # 2020-09-18 16:48:17.822981
# Get timezone aware current date time
print(datetime.datetime.now().astimezone()) # 2020-09-18 22:18:43.722671+05:30
# Get timezone aware current date time in UTC
print(datetime.datetime.utcnow().astimezone(timezone.utc)) # 2020-09-18 16:49:06.660544+00:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment