Date and Time Data Types and Tools - Time Series
The Pandas library gives us a lot of date and time tools to study time series data.
Let us begin with the basic DateTime module in python.
Code
from datetime import datetime
from datetime import timedelta
Let us try to print the date and time at the current moment.
Code
now = datetime.now()
Code
now
Output

The date & time output is up to in microseconds.
We can extract any detail such as year, month, day, etc. from the above datetime output.
Code
print(now.year, now.month, now.day)
Output

Calculating the difference between two dates
The datetime.timedelta() in python, can store the difference between two dates, accurately in microseconds.
Code
diff = datetime(2020, 8, 12) — datetime(2020, 7, 12, 10)
Code
diff
Output

We can extract any detail, such as days or seconds from the above datetime.timedelta() object.
Code
print(diff.days, diff.seconds)
Output

Adding (or Subtracting) a timedelta to a datetime object
We can add datetime.timedelta() to datetime() object.
Code
date1 = datetime(2020, 8, 12)
Let us try to add 12 days to the above datetime object.
Code
date1 + timedelta(12)
Output

Let us try to subtract 12 days from the same above datetime object.
Code
date1 — timedelta(12)
Output
