Manish Barnwal

...just another human

Working with dates in Python

I cringe every time I see a date type column in the data. And you may ask why so? Date columns need some methods applied to them

The reason is I don't normally see date columns in the data I work with so I don't remember the functions and methods that work on dates to get meaningful columns out of the date column in the data.

from datetime import datetime

To convert a string to date:

`datetime.strptime('string', 'format_of_the_string')``

d1 = datetime.strptime('2017-02-02', '%Y-%m-%d')

`d2 = datetime.strptime('2018-06-17', '%Y-%m-%d')``

Find the number of days between two dates `delta = d2 - d1 delta.days ``

Difference between today and some date today = delta = today - d2 delta.days

Advertiser Disclosure: This post contains affiliate links, which means I receive a commission if you make a purchase using this link. Your purchase helps support my work.