Python Datetime - The 9 Tips You Shall Know

Image for post
Image for post
Photo by William Iven on Unsplash

Introduction

Prerequisite

pip install python-dateutil

Various ways to get current date and time

>>>from datetime import datetime 
>>>import time #Local timezone
>>>datetime.now()
datetime.datetime(2020, 10, 24, 21, 31, 11, 761666)
>>>datetime.today()
datetime.datetime(2020, 10, 24, 21, 31, 12, 139719)
>>>datetime.fromtimestamp(time.time())
datetime.datetime(2020, 10, 24, 21, 31, 12, 559183)
#Not suggested >>>datetime.fromtimestamp(time.mktime(time.localtime()))
datetime.datetime(2020, 10, 24, 21, 33, 5)
#UTC timezone
>>>datetime.now(timezone.utc)
datetime.datetime(2020, 10, 24, 13, 31, 13, 443442, tzinfo=datetime.timezone.utc)
>>>datetime.utcnow()
datetime.datetime(2020, 10, 24, 13, 31, 14, 240517)
>>>datetime.now().date() 
datetime.date(2020, 10, 24)

Get year, month, day and time from Python datetime

>>>TODAY = datetime.today() 
>>>TODAY.year, TODAY.month, TODAY.day, TODAY.hour, TODAY.minute, TODAY.second, TODAY.microsecond
(2020, 10, 24, 21, 36, 35, 842689)
#Monday is 0 and Sunday is 6
>>>TODAY.weekday()
5
#Monday is 1 and Sunday is 7
>>>TODAY.isoweekday()
6
#Return year, weekno, and weekday
>>>TODAY.isocalendar()
(2020, 43, 6)

Date plus or minus X days

>>>timedelta(days=1, seconds=50, microseconds=1000, milliseconds=1000, minutes=10, hours=6, weeks=1) datetime.timedelta(days=8, seconds=22251, microseconds=1000)
>>>tomorrow = datetime.today().date() + timedelta(days=1) 
datetime.date(2020, 10, 25)
>>>yesterday = datetime.today().date() + timedelta(days=-1) 
datetime.date(2020, 10, 23)
>>>tomorrow - yesterday 
datetime.timedelta(days=2)

Get the first day of the month

>>>datetime.today().date().replace(day=1) 
datetime.date(2020, 10, 1)

Format date with strftime and strptime

>>>datetime.now().strftime("%Y-%b-%d %H:%M:%S") 
'2020-Oct-25 20:35:54
>>>datetime.strptime("Oct 25 2020 08:10:00", "%b %d %Y %H:%M:%S") 
datetime.datetime(2020, 10, 25, 8, 10)

Create time zone aware date

>>>singapore_tz = timezone(timedelta(hours=8), name="SGT") 
>>>sg_time_now = datetime.now(tz=singapore_tz)
datetime.datetime(2020, 10, 24, 22, 31, 6, 554991, tzinfo=datetime.timezone(datetime.timedelta(seconds=28800), 'SGT'))
>>>import dateutil #time zone database name from IANA 
>>>sh_tz = dateutil.tz.gettz('Asia/Shanghai')
>>>datetime(2020, 10, 24, 22, tzinfo = sh_tz) datetime.datetime(2020, 10, 24, 22, 0, tzinfo=tzfile('PRC'))
#windows time zone names
>>>cn_tz = dateutil.tz.gettz('China Standard Time')
>>>datetime(2020, 10, 24, 22, tzinfo = cn_tz)
datetime.datetime(2020, 10, 24, 22, 0, tzinfo=tzwin('China Standard Time'))

Get a date by relative period

>>>datetime(2019, 5, 1) + timedelta(days=365) datetime.datetime(2020, 4, 30, 0, 0)
>>>from dateutil.relativedelta 
>>>import relativedelta
>>>datetime(2019, 5, 1) + relativedelta(years=1)
datetime.datetime(2020, 5, 1, 0, 0)
>>>datetime.today() + relativedelta(years=1, months=1, days=10, hours=10) 
datetime.datetime(2021, 12, 5, 8, 49, 31, 386813)
>>>datetime.today() + relativedelta(weekday=calendar.SATURDAY) datetime.datetime(2020, 10, 24, 15, 16, 10, 502191)

List out all the weekdays

>>>from dateutil.rrule import rrule, DAILY, MO, TU, WE, TH, FR 
>>>from dateutil.parser import parse
>>>list(rrule(DAILY, interval=1, byweekday=[MO, TU, WE, TH, FR], dtstart=datetime.now().date(), until=datetime(2020, 11, 2)))

[datetime.datetime(2020, 10, 26, 0, 0),
datetime.datetime(2020, 10, 27, 0, 0),
datetime.datetime(2020, 10, 28, 0, 0),
datetime.datetime(2020, 10, 29, 0, 0),
datetime.datetime(2020, 10, 30, 0, 0),
datetime.datetime(2020, 11, 2, 0, 0)]
>>>list(rrule(DAILY, interval=2, byminute=15, count=4, dtstart=parse("20201024T090000"))) [datetime.datetime(2020, 10, 24, 9, 15), 
datetime.datetime(2020, 10, 26, 9, 15),
datetime.datetime(2020, 10, 28, 9, 15),
datetime.datetime(2020, 10, 30, 9, 15)]

Get a list of business days

holidays = [ 
datetime(2020, 7, 10,),
datetime(2020, 7, 31,),
datetime(2020, 8, 10,)
]
r = rrule(DAILY, interval=1,
byweekday=[MO, TU, WE, TH, FR],
dtstart=datetime(2020, 7, 10),
until=datetime(2020, 8, 1))
rs = rrule.rruleset()
rs.rrule(r) for d in holidays:
rs.exdate(d)
print(list(rs))
Image for post
Image for post

Conclusion

Written by

Resources and tutorials for python, data science and automation solutions

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store