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 into date python
date_str = '09-19-2018'
date_object = datetime.strptime(date_str, '%m-%d-%Y').date()
print(type(date_object))
print(date_object) # printed in default formatting
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)
convert timezone python
import pytz
from dateutil import tz
from datetime import datetime, timedelta
# must read source link else Bug:
NYC_p = pytz.timezone('America/New_York') # 1.53 µs
NYC_d = tz.gettz('America/New_York') # 863 ns
# tz Awareness
dt_p = NYC_p.localize(datetime(2018, 11, 1)) # 35.4 µs
dt_d = datetime(2018, 11, 1, tzinfo=NYC_d) # 1.38 µs
dt_p.utcoffset() # 655 ns
dt_d.utcoffset() # 13.9 µs
LA_p = pytz.timezone('America/Los_Angeles')
LA_d = tz.gettz('America/Los_Angeles')
# tz Conversion
NYC_p.localize(datetime(2018, 11, 1)).astimezone(LA_p) # 44.8 µs
datetime(2018, 11, 1, tzinfo=NYC_d).astimzone(LA_d) # 32.8 µs
# ~~~~~ Date Arithmetic
dt_winter = datetime(2018, 2, 14, 12, tzinfo=NYC_d)
dt_spring = dt_winter + timedelta(days=60)
print(dt_spring)
# 2018-04-15 12:00:00-04:00 # correct
dt_winter = NYC_p.localize(datetime(2018, 2, 14, 12)) # potential Bug vector1
dt_spring = dt_winter + timedelta(days=60)
print(dt_spring)
# 2018-04-15 12:00:00-05:00 # incorrect. subtle gotcha!
print(NYC_p.normalize(dt_spring)) # potential Bug vector2
# 2018-04-15 13:00:00-04:00 # correct
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