Import date, time and datetime classes
from datetime import date
from datetime import time
from datetime import datetime
## Using objects of class type date, time, datetime
today = date.today()
print ( "Today's date is", today )
print ( "Today :", today.month, today.day, today.year )
weekday = "Sunday" if today.weekday() == 6 else ""
weekday = "Monday" if today.weekday() == 0 else weekday
weekday = "Tuesday" if today.weekday() == 1 else weekday
weekday = "Wednesday" if today.weekday() == 2 else weekday
weekday = "Thursday" if today.weekday() == 3 else weekday
weekday = "Friday" if today.weekday() == 4 else weekday
weekday = "Saturday" if today.weekday() == 5 else weekday
print ( "Today : {} / {}-{}-{}".format ( weekday, today.month, today.day, today.year ))
today = datetime.now()
print ( "Now is", today )
days = [ "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" ]
wkday = date.weekday (today) ## returns the same as today.weekday()
print ( "Today : {} / {}-{}-{}".format ( days[wkday], today.month, today.day, today.year ))
Datetime formatting
print ( today.strftime ("%Y") ) ## full year, ex. 2019
print ( today.strftime ("%a") ) ## abbreviated day, ex. Tue
print ( today.strftime ("%d") ) ## day number, ex. 18
print ( today.strftime ("%B") ) ## full month, ex. December
print ( today.strftime ("%y") ) ## abbreviated year, ex. 19 for year 2019
print ( today.strftime ("%c") ) ## locale's date and time, ex. Tue Dec 18 21:36:09 2019
print ( today.strftime ("%x") ) ## locale's date, ex. 12/18/19
print ( today.strftime ("%X") ) ## locale's time, ex. 21:36:09
print ( today.strftime ("%A, %d %b, %Y") ) ## prints "Tuesday, 18 Dec, 2018"
# for time formatting #
# %I/%H - 12/24 hour system, %M - minute, %S - second, %p - locale's AM/PM
print ( today.strftime ("%H:%M:%S %p") ) ## 24-Hour:Minute:Second:AM/PM
print ( today.strftime ("%I:%M") ) ## 12-Hour:Minute
Timedeltas
# timedeltas #
from datetime import timedelta
print ( "One year from today :", datetime.now() + timedelta (days=365) )
print ( timedelta (days=222, hours=3, minutes=4) )
print ( "Today:", datetime.now() )
print ( "3 weeks and 3 days from now :", datetime.now() + timedelta(weeks = 3, days=3) )
print ( "3 weeks and 3 days from now :" + str(datetime.now() + timedelta(weeks = 3, days=3)) )
# how many days till Halloween
t1 = datetime.now() - timedelta(weeks=2)
t1str = t1.strftime ("%A %B %d, %Y")
print ( t1str + " is two weeks old." )
# how many days till Halloween
today = date.today()
halloween = date(today.year, 10,30)
if halloween < today:
print ( "Halloween has passed by {} days".format( (today - halloween).days ))
halloween = halloween.replace (year=today.year + 1)
time_to_halloween = abs(halloween - today)
print ( "There are", time_to_halloween.days, "more days to Halloween." )
Calendars
import calendar
# To start the calendar on Sunday as first column
c = calendar.TextCalendar (calendar.SUNDAY)
cstr = c.formatmonth (2019, 1, 0, 0)
print (cstr)
# returns multiple '0', say two '0' if 2019/1/1 starts on Tuesday
for i in c.itermonthdays (2019, 1):
print (i)
for name in calendar.month_name:
print (name)
for name in calendar.day_name:
print (name)
ta = datetime.datetime (year=2018, month=1, day=1)
tb = datetime.datetime (year=2018, month=2, day=1)
tb-ta
ta-tb
print (tb-ta)
No comments:
Post a Comment