Python Datetime Module - How to work with Dates, Times, Timedeltas, and Timezones
In python DateTime library is very useful for in program inside using date,time,month,dayofmonth,weekday, isoweekday
import datetime
d = datetime.date(2020,7,13)
print(d)
you can see output 2020-07-13 and you set in syntex in month set 07 you put syntex error.
if you knowing today local date
import datetime
tday = datetime.date.today()
print(tday)
In output you can see today's date.
import datetime
tday = datetime.date.today()
print(tday.day) <- here you can use month,day etc.
if you use date of the week it's two type
import datetime
tday = datetime.date.today()
print(tday.weekday())
print(tday.isoweekday())
you see in to the output 1 and 2
For weekday Moday is o and sunday is 6
For isoweekday is 1 and sunday is 7
Now create timedelta for one week away for 7 day
import datetime
tday = datetime.date.today()
tdelta = datetime.timedelta(days=7)
print(tday + tdelta)
In output you can see date of 7 days
import datetime
tday = datetime.date.today()
tdelta = datetime.timedelta(days=7)
# print(tday + tdelta)
# date2 = date1 + timedelta
# timedelta = date1 + date2
Let's take a example how many day until my birthday
import datetime
tday = datetime.date.today()
tdelta = datetime.timedelta(days=7)
# print(tday + tdelta)
# date2 = date1 + timedelta
# timedelta = date1 + date2
bday = datetime.date(1999,6,6)
till_bday = bday - tday()
print(till_bday) <- you can know which between day print(till_bday) <- you can know which second in your between birthday print(till_bday.total_seconds())
as you can see as a date now you can see for time
import datetime
t = datetime.time(9,10,45,10000) <- (hour,minutes,second,microseconds)
print(t) <-in you can only print hour you just add print(t.hour)
You can see output like 09:10:45.10000
You can access date and time with hour minutes second without date.time use you can access both
import datetime
dt = datetime.datetime(2020,7,13,9,10,45,10000) <- in this as your choice you can access only date print(dt.date())
You can see the output with date and with time
PYTZ:
import datetime
import pytz
dt = datetime.datetime(2020,7,13,10,27,50,tzinfo=pytz.UTC)
print(dt)
You can seet output like 2020-07-13 10:27:50 +00.00
dt_now = datetime.datetime.now(tzinfo=pytz.UTC)
print(dt_now) Now you can see current datetimezone
dt_utcnow = datetime.datetime.utcnow().replace(tzinfo=pytz.UTC)
print(dt_utcnow)
How can you convert into many timezone🤔:
import datetime
import pytz
dt_utcnow = datetime.datetime.now(tzinfo=pytz.UTC)
print(dt_utcnow)
dt_mtn = dt_utcnow.astimezone(pytz.timezone('US/Mountain'))
print(dt_mtn)
Now you can see output
import datetime
import pytz
dt_utcnow = datetime.datetime.now(tzinfo=pytz.UTC)
#print(dt_utcnow)
dt_mtn = dt_utcnow.astimezone(pytz.timezone('US/Mountain'))
#print(dt_mtn)
for tz in pytz.all.timezones:
print(tz)
->OUTPUT:-Now you can see all time zone in the us
import datetime
import pytz
dt_utcnow = datetime.datetime.now(tzinfo=pytz.UTC)
print(dt_utcnow)
dt_mtn = dt_utcnow.astimezone(pytz.timezone('US/Mountain'))
print(dt_mtn)
Create New Local time zone doesn't have time zone information
import datetime
import pytz
dt_utcnow = datetime,datetime.now(tzinfo=pytz.UTC)
print(dt_utcnow)
dt_mtn = datetime.datetime.now()
print(dt_mtn) <- You run this program but you can't see time zone only you can see time perticular run time now next step to do.
import datetime
import pytz
dt_utcnow = datetime,datetime.now(tzinfo=pytz.UTC)
print(dt_utcnow)
dt_mtn = datetime.datetime.now()
# dt_east = dt_mtn.astimezone(pytz.timezone('US/Eastern')) <-In for example you know east timezone so put like that command put you threw error when you run a program. so this code commented because this function compiler error thru call locally function.
mtn_tz = pytz.timezone('US/Mountain')
dt_mtz = mtz_tz.localize(dt_mtn) <- USing this command now you can see inside output timezone
print(dt_mtn)
Now you can uncomment command and try to run command
import datetime
import pytz
dt_utcnow = datetime,datetime.now(tzinfo=pytz.UTC)
print(dt_utcnow)
dt_mtn = datetime.datetime.now()
dt_east = dt_mtn.astimezone(pytz.timezone('US/Eastern')) <- Uncomment this command
mtn_tz = pytz.timezone('US/Mountain')
dt_mtz = mtz_tz.localize(dt_mtn)
print(dt_east)
#print(dt_mtn)
Best Way to writing code for datetime:-
import datetime
import pytz
dt_mtn = datetime,datetime.now(tzinfo=pytz.timezone('US/Mountain'))
#print(dt_mtn.strftime('%B %d,%Y')) <-You can see output like july 26,2020
You print as string datetime you can see like this Strftime -- Date time to String
dt_str ='july 26,2020
dt = datetime.datetime.strptime(dt_str,'%B %d,%Y')
print(dt)
Strftime -- Date time to String
Strptime -- String to Datetime
Comments
Post a Comment