Date And Time Basics

Want to learn more? I recommend these Python books: Python for Data Analysis, Python Data Science Handbook, and Introduction to Machine Learning with Python.

# Import modules
from datetime import datetime
from datetime import timedelta
# Create a variable with the current time
now = datetime.now()
now
datetime.datetime(2014, 5, 11, 20, 5, 11, 688051)
# The current year
now.year
2014
# The current month
now.month
5
# The current day
now.day
11
# The current hour
now.hour
20
# The current minute
now.minute
5
# The difference between two dates
delta = datetime(2011, 1, 7) - datetime(2011, 1, 6)
delta
datetime.timedelta(1)
# The difference days
delta.days
1
# The difference seconds
delta.seconds
0
# Create a time
start = datetime(2011, 1, 7)
# Add twelve days to the time
start + timedelta(12)
datetime.datetime(2011, 1, 19, 0, 0)