Skip to content

Instantly share code, notes, and snippets.

@carlosequiz
Created January 5, 2021 14:14
Show Gist options
  • Save carlosequiz/d50758dc8c3731a599ccc6734317b47b to your computer and use it in GitHub Desktop.
Save carlosequiz/d50758dc8c3731a599ccc6734317b47b to your computer and use it in GitHub Desktop.
Dealing with Timezones and datetime
import datetime as dt
import pytz
date_time_str = '2021-01-06T01:10:54.269'
date_time_obj = dt.datetime.strptime(date_time_str, '%Y-%m-%dT%H:%M:%S.%f')
timezone = pytz.timezone('UTC')
timezone_date_time_obj = timezone.localize(date_time_obj)
timezone_date_time_now = dt.datetime.now(pytz.utc)
print(timezone_date_time_obj)
print(timezone_date_time_obj.tzinfo)
print(timezone_date_time_now)
print(timezone_date_time_now.tzinfo)
if timezone_date_time_obj < timezone_date_time_now:
print("It's before")
else:
print("It's after")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment