0% found this document useful (0 votes)
37 views

Dates in Python

This document discusses working with dates and times in Python. It covers creating date objects from strings, extracting attributes from dates like year and month, performing math operations on dates like subtraction and addition, converting dates to strings in ISO 8601 and other formats using strftime, and sorting dates. The goal is to demonstrate how to work with dates and times in Python for tasks like finding the elapsed time between dates, checking date order, determining the weekday, and filtering dates by range.

Uploaded by

Farhan Tanvir
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Dates in Python

This document discusses working with dates and times in Python. It covers creating date objects from strings, extracting attributes from dates like year and month, performing math operations on dates like subtraction and addition, converting dates to strings in ISO 8601 and other formats using strftime, and sorting dates. The goal is to demonstrate how to work with dates and times in Python for tasks like finding the elapsed time between dates, checking date order, determining the weekday, and filtering dates by range.

Uploaded by

Farhan Tanvir
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Dates in Python

W O R K I N G W I T H D AT E S A N D T I M E S I N P Y T H O N

Max Shron
Data Scientist and Author
Course overview
Chapter 1: Dates and Calendars

Chapter 2: Combining Dates and Times

Chapter 3: Time zones and Daylight Saving

Chapter 4: Dates and Times in Pandas

WORKING WITH DATES AND TIMES IN PYTHON


Dates in Python

WORKING WITH DATES AND TIMES IN PYTHON


Why do we need a date class in Python?
two_hurricanes = ["10/7/2016", "6/21/2017"]

How would you:

Figure out how many days had elapsed?

Check that they were in order from earliest to latest?

Know which day of the week each was?

Filter out hurricanes which happened between certain dates?

WORKING WITH DATES AND TIMES IN PYTHON


Creating date objects
# Import date
from datetime import date

# Create dates
two_hurricanes_dates = [date(2016, 10, 7), date(2017, 6, 21)]

WORKING WITH DATES AND TIMES IN PYTHON


Attributes of a date
# Import date
from datetime import date

# Create dates
two_hurricanes_dates = [date(2016, 10, 7), date(2017, 6, 21)]

print(two_hurricanes_dates[0].year)
print(two_hurricanes_dates[0].month)
print(two_hurricanes_dates[0].day)

2016
10
7

WORKING WITH DATES AND TIMES IN PYTHON


Finding the weekday of a date
print(two_hurricanes_dates[0].weekday())

Weekdays in Python
0 = Monday

1 = Tuesday

2 = Wednesday

...
6 = Sunday

WORKING WITH DATES AND TIMES IN PYTHON


Dates in Python
W O R K I N G W I T H D AT E S A N D T I M E S I N P Y T H O N
Math with Dates
W O R K I N G W I T H D AT E S A N D T I M E S I N P Y T H O N

Max Shron
Data Scientist and Author
Math with dates

# Example numbers
a = 11
b = 14
l = [a, b]

# Find the least least in the list


print(min(l))
11

WORKING WITH DATES AND TIMES IN PYTHON


Math with dates

# Subtract two numbers


print(b - a)
3

# Add 3 to a
print(a + 3)
14

WORKING WITH DATES AND TIMES IN PYTHON


Math with dates

WORKING WITH DATES AND TIMES IN PYTHON


Math with dates

# Import date
from datetime import date

# Create our dates


d1 = date(2017, 11, 5)
d2 = date(2017, 12, 4)
l = [d1, d2]

print(min(l))
2017-11-05

WORKING WITH DATES AND TIMES IN PYTHON


Math with dates

# Subtract two dates


delta = d2 - d1

print(delta.days)
29

WORKING WITH DATES AND TIMES IN PYTHON


Math with dates

# Import timedelta
from datetime import timedelta

# Create a 29 day timedelta


td = timedelta(days=29)
print(d1 + td)
2017-12-04

WORKING WITH DATES AND TIMES IN PYTHON


Incrementing variables with +=
# Initialize x to be zero # Initialize x to be zero
x = 0 x = 0

# Increment x # Increment x
x = x + 1 x += 1

print(x) print(x)
1 1

WORKING WITH DATES AND TIMES IN PYTHON


Let's Practice!
W O R K I N G W I T H D AT E S A N D T I M E S I N P Y T H O N
Turning dates into
strings
W O R K I N G W I T H D AT E S A N D T I M E S I N P Y T H O N

Max Shron
Data Scientist and Author
ISO 8601 format
from datetime import date

# Example date
d = date(2017, 11, 5)

# ISO format: YYYY-MM-DD


print(d)

2017-11-05

# Express the date in ISO 8601 format and put it in a list


print( [d.isoformat()] )

['2017-11-05']

WORKING WITH DATES AND TIMES IN PYTHON


ISO 8601 format
# A few dates that computers once had trouble with
some_dates = ['2000-01-01', '1999-12-31']

# Print them in order


print(sorted(some_dates))

['1999-12-31', '2000-01-01']

WORKING WITH DATES AND TIMES IN PYTHON


Every other format
d.strftime()

WORKING WITH DATES AND TIMES IN PYTHON


Every other format: strftime
# Example date
d = date(2017, 1, 5)

print(d.strftime("%Y"))

2017

# Format string with more text in it


print(d.strftime("Year is %Y"))

Year is 2017

WORKING WITH DATES AND TIMES IN PYTHON


Every other format: strftime
# Format: YYYY/MM/DD
print(d.strftime("%Y/%m/%d"))

2017/01/05

WORKING WITH DATES AND TIMES IN PYTHON


Turning dates into
strings
W O R K I N G W I T H D AT E S A N D T I M E S I N P Y T H O N

You might also like