convert into date python
from datetime import datetime
datetime_str = '09/19/18 13:55:26'
datetime_object = datetime.strptime(datetime_str, '%m/%d/%y %H:%M:%S')
print(type(datetime_object))
print(datetime_object) # printed in default format
convert into date python
from datetime import datetime
datetime_str = '09/19/18 13:55:26'
datetime_object = datetime.strptime(datetime_str, '%m/%d/%y %H:%M:%S')
print(type(datetime_object))
print(datetime_object) # printed in default format
convert timezone python
from datetime import datetime
from pytz import timezone, all_timezones
# must read else Bug:
# https://blog.ganssle.io/articles/2018/03/pytz-fastest-footgun.html
# aware dt-obj
dt_obj = datetime.strptime('2021-05-19T01:55:10+0000', '%Y-%m-%dT%H:%M:%S%z')
# double confirmaiton: aware dt-obj
dt_obj.tzinfo
# correct
dt_obj.astimezone(timezone('US/Pacific'))
# correct
dt_obj.replace(tzinfo=timezone('UTC')).astimezone(timezone('US/Pacific'))
# confirmation: desired tz
dt_obj.replace(tzinfo=timezone('UTC')).astimezone(timezone('US/Pacific')).tzinfo
# ~~~~~~~
# naive datetime object
datetime.utcnow()
# confirmation: naive dt-obj
print(datetime.utcnow().tzinfo)
# incorrect because started with naive datetime object
datetime.utcnow().astimezone(timezone('US/Pacific'))
# correct because add/replace tzinfo of initial dt-obj before conversion to desired tz
# SEE article link above. The following is probably correct only bc starting w/ utc.
datetime.utcnow().replace(tzinfo=timezone('UTC')).astimezone(timezone('US/Pacific'))
# confirmation: aware dt-obj
datetime.utcnow().replace(tzinfo=timezone('UTC')).astimezone(timezone('US/Pacific')).tzinfo
from dateutil import tz
dt=datetime(2018, 11, 1,20,0,0)
print(dt)
# 2018-11-01 20:00:00
print(dt.replace(tzinfo=tz.gettz('UTC')).astimezone(tz.gettz('US/Eastern')))
# 2018-11-01 16:00:00-04:00
print(datetime(2018, 11, 1,20,0,0,tzinfo=tz.gettz('UTC')).astimezone(tz.gettz('US/Eastern')))
# 2018-11-01 16:00:00-04:00
print(dt.replace(tzinfo=timezone('UTC')).astimezone(timezone('US/Eastern')))
# 2018-11-01 16:00:00-04:00
print(datetime(2018, 11, 1,20,0,0,tzinfo=timezone('UTC')).astimezone(timezone('US/Eastern')))
# 2018-11-01 16:00:00-04:00
print(timezone('UTC').localize(dt).astimezone(timezone('US/Eastern')))
# 2018-11-01 16:00:00-04:00
# ~~ VS ~~
dt=datetime(2018, 11, 1,16,0,0)
print(dt)
# 2018-11-01 16:00:00
print(dt.replace(tzinfo=tz.gettz('US/Eastern')).astimezone(tz.gettz('US/Pacific')))
# 2018-11-01 13:00:00-07:00
print(datetime(2018, 11, 1,16,0,0,tzinfo=tz.gettz('US/Eastern')).astimezone(tz.gettz('US/Pacific')))
# 2018-11-01 13:00:00-07:00
print(dt.replace(tzinfo=timezone('US/Eastern')).astimezone(timezone('US/Pacific'))) # Bug vector
# 2018-11-01 13:56:00-07:00 # incorrect!
print(datetime(2018, 11, 1,16,0,0,tzinfo=timezone('US/Eastern')).astimezone(timezone('US/Pacific'))) # Bug vector
# 2018-11-01 13:56:00-07:00 # incorrect!
print(timezone('US/Eastern').localize(dt).astimezone(timezone('US/Pacific')))
# 2018-11-01 13:00:00-07:00
# ~~~~~~~~~~~
# len(all_timezones) == 593
for zone in all_timezones:
print(zone)
python datetime from string
from datetime import datetime
datetime_object = datetime.strptime('Jun 1 2005 1:33PM', '%b %d %Y %I:%M%p')
python set timezone of datetime
datetime_with_timezone = datetime.datetime(2019, 2, 3, 6, 30, 15, 0, pytz.UTC)
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us