Shift time in
To get your new date in
To turn
#python #epoch #datetime #time #timedelta #strftime
python
using datetime
& timedelta
:import datetime
from datetime import timedelta
date1 = datetime.datetime.utcnow()
# output of date1: datetime.datetime(2017, 11, 8, 10, 24, 25, 19492)
date2 = date1 + timedelta(days=1)
# output of date2: datetime.datetime(2017, 11, 9, 10, 24, 25, 19492)
To get your new date in
EPOCH
:at_epoch = (date2 - datetime.datetime(1970, 1, 1)).total_seconds()
# output of subtract: 1510223065.019492
To turn
EPOCH
to datetime
:import time
from_epoch = time.localtime(1510223065.019492)
# output: time.struct_time(tm_year=2017, tm_mon=11, tm_mday=9, tm_hour=13, tm_min=54, tm_sec=25, tm_wday=3, tm_yday=313, tm_isdst=0)
formatted_date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1510223065.019492))
# output: 2017-11-09 13:54:25
#python #epoch #datetime #time #timedelta #strftime
It is always a best practice to store dates in UTC. Do not ever store dates in your local timezone in which you will go to hell in your programming experience!
Given that you have saved your dates in UTC and would like to convert them into
#python #date #utc #strftime #dateutil #timezone #pytz
Given that you have saved your dates in UTC and would like to convert them into
Asia/Tehran
date in Python
do like below:date_format = '%Y-%m-%d %H:%M:%S'
local_tz = pytz.timezone('Asia/Tehran')
submit_time = dateutil.parser.parse(utc_date)
submit_time = submit_time.replace(tzinfo=pytz.utc).astimezone(local_tz)
output = submit_time.strftime(date_format)
#python #date #utc #strftime #dateutil #timezone #pytz