Chapter 1
Chapter 1
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
# 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
Weekdays in Python
0 = Monday
1 = Tuesday
2 = Wednesday
...
6 = Sunday
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
# Add 3 to a
print(a + 3)
14
# 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
print(delta.days)
29
# Import timedelta
from datetime import timedelta
# Create a 29 day timedelta
td = timedelta(days=29)
print(d1 + td)
2017-12-04
# Increment x # Increment x
x = x + 1 x += 1
print(x) print(x)
1 1
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
['2017-11-05']
['1999-12-31', '2000-01-01']
print(d.strftime("%Y"))
2017
Year is 2017
2017/01/05