Python Lesson 15: Dealing with dates and time

  1. Get Current Date
  2. Get Current Date and Time
  3. Create and set a Date
  4. Create and set a Date and Time
  5. Date String Formats
  6. Output a Formatted String from a DateTime object
  7. Output a Date from a Formatted String
  8. Get the difference between 2 dates
  9. Add days to a date to calculate a future date
  10. Handle time zones

The DateTime module is useful for handling dates in Python.

1) Get Current Date

from datetime import date
#Get Current Date
today = date.today()
print(today)
Notebook Output

2) Get Current Date and Time

from datetime import datetime
#Get Current Date and Time
now = datetime.now()
print(now)
Notebook Output

3) Create and set a Date

from datetime import date
#Get Current Date
mydate = date(2019,2,8)
print(mydate)
Notebook Output

4) Create and set a Date and Time

from datetime import datetime
#Get Current Date and Time
now = datetime(2019,2,8,18,30,0)
print(now)
Notebook Output

5) Date String Formats

Code . Definition Example
%a Weekday abbreviated name. Mon
%A Weekday full name. Monday
%w Weekday as a number, 0 is Sunday, 1 is Monday, 2 is Tuesday, 3 is Wednesday, 4 is Thursday, 5 is Friday, 6 is Saturday 1
%d Day of the month as a zero-padded decimal number. 30
%-d Day of the month as a number. 30
%b Month as abbreviated name. Sep
%B Month as full name. September
%m Month as a zero-padded decimal number. 9
%-m Month as a number 9
%y Year without century as a zero-padded number. 13
%Y Year with century as a number. 2013
%H Hour (24-hour clock) as a zero-padded decimal number. 7
%-H Hour (24-hour clock) as a number 7
%I Hour (12-hour clock) as a zero-padded number. 7
%-I Hour (12-hour clock) as a number. 7
%p Locale’s equivalent of either AM or PM. AM
%M Minute as a zero-padded number. 6
%-M Minute as a number 6
%S Second as a zero-padded number. 5

6) Output a Formatted String from a DateTime object

Use the strftime() method from datetime to create a formatted string from a datetime object.

from datetime import datetime
#Output a formatted string from a datetime object
now = datetime(2019,2,8,18,30,0)
print(now.strftime("%m/%d/%Y"))
Notebook Output

7) Output a Date from a Formatted String

Use the strptime() method from datetime to create a datetime from a formatted string.

from datetime import datetime
#Convert a formatted string to a datetime object
datestring = "2019-02-19"
print(datetime.strptime(datestring,"%Y-%m-%d"))
Notebook Output

8) Get the difference between 2 dates

Use the “-” operator to take the difference between 2 dates.
Outputs timedelta object.

from datetime import datetime,date

today = date(2019,2,8)
futuredate = date(2019,11,9)
diff = futuredate - today
print(diff)
Notebook Output

9) Add days to a date to calculate a future date

from datetime import date,timedelta
#add 100 days to todays date
t1 = timedelta(days = 100)
today = date.today()
futuredate = today + t1
print(futuredate)
Notebook Output

10) Handle time zones

Use the pytz module to handle time zones.

from datetime import datetime
import pytz
#Get local time in London Time

local = datetime.now()
print("Local:", local.strftime("%m/%d/%Y, %H:%M:%S"))

tz_London = pytz.timezone('Europe/London')
datetime_London = datetime.now(tz_London)
print("London:", datetime_London.strftime("%m/%d/%Y, %H:%M:%S"))
Notebook Output

Recommended Books