Skip to content

Instantly share code, notes, and snippets.

@monroy95
Forked from amalgjose/DateDifference.py
Created January 28, 2020 03:14
Show Gist options
  • Save monroy95/da75b89d098eb8b688ca0db0cb34508d to your computer and use it in GitHub Desktop.
Save monroy95/da75b89d098eb8b688ca0db0cb34508d to your computer and use it in GitHub Desktop.
This is a very simple python code snippet for calculating the difference between two dates or timestamps. This will calculate the difference in terms of number of years, months, days, hours, minutes etc.
__author__ = 'Amal G Jose'
from datetime import datetime
from dateutil import relativedelta
##Aug 7 1989 8:10 pm
date_1 = datetime(1989, 8, 7, 20, 10)
##Dec 5 1990 5:20 am
date_2 = datetime(1990, 12, 5, 5, 20)
#This will find the difference between the two dates
difference = relativedelta.relativedelta(date_2, date_1)
years = difference.years
months = difference.months
days = difference.days
hours = difference.hours
minutes = difference.minutes
print "Difference is %s year, %s months, %s days, %s hours, %s minutes " %(years, months, days, hours, minutes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment