How to calculate number of days between two given dates - python

If I have two dates (ex. '8/18/2008' and '9/26/2008'), what is the best way to get the number of days between these two dates?

If you have two date objects, you can just subtract them, which computes a timedelta object.
from datetime import date
d0 = date(2008, 8, 18)
d1 = date(2008, 9, 26)
delta = d1 - d0
print(delta.days)
The relevant section of the docs:
https://docs.python.org/library/datetime.html.
See this answer for another example.

Using the power of datetime:
from datetime import datetime
date_format = "%m/%d/%Y"
a = datetime.strptime('8/18/2008', date_format)
b = datetime.strptime('9/26/2008', date_format)
delta = b - a
print delta.days # that's it

Days until Christmas:
>>> import datetime
>>> today = datetime.date.today()
>>> someday = datetime.date(2008, 12, 25)
>>> diff = someday - today
>>> diff.days
86
More arithmetic here.

everyone has answered excellently using the date,
let me try to answer it using pandas
dt = pd.to_datetime('2008/08/18', format='%Y/%m/%d')
dt1 = pd.to_datetime('2008/09/26', format='%Y/%m/%d')
(dt1-dt).days
This will give the answer.
In case one of the input is dataframe column. simply use dt.days in place of days
(dt1-dt).dt.days

You want the datetime module.
>>> from datetime import datetime
>>> datetime(2008,08,18) - datetime(2008,09,26)
datetime.timedelta(4)
Another example:
>>> import datetime
>>> today = datetime.date.today()
>>> print(today)
2008-09-01
>>> last_year = datetime.date(2007, 9, 1)
>>> print(today - last_year)
366 days, 0:00:00
As pointed out here

from datetime import datetime
start_date = datetime.strptime('8/18/2008', "%m/%d/%Y")
end_date = datetime.strptime('9/26/2008', "%m/%d/%Y")
print abs((end_date-start_date).days)

It also can be easily done with arrow:
import arrow
a = arrow.get('2017-05-09')
b = arrow.get('2017-05-11')
delta = (b-a)
print delta.days
For reference: http://arrow.readthedocs.io/en/latest/

without using Lib just pure code:
#Calculate the Days between Two Date
daysOfMonths = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
def isLeapYear(year):
# Pseudo code for this algorithm is found at
# http://en.wikipedia.org/wiki/Leap_year#Algorithm
## if (year is not divisible by 4) then (it is a common Year)
#else if (year is not divisable by 100) then (ut us a leap year)
#else if (year is not disible by 400) then (it is a common year)
#else(it is aleap year)
return (year % 4 == 0 and year % 100 != 0) or year % 400 == 0
def Count_Days(year1, month1, day1):
if month1 ==2:
if isLeapYear(year1):
if day1 < daysOfMonths[month1-1]+1:
return year1, month1, day1+1
else:
if month1 ==12:
return year1+1,1,1
else:
return year1, month1 +1 , 1
else:
if day1 < daysOfMonths[month1-1]:
return year1, month1, day1+1
else:
if month1 ==12:
return year1+1,1,1
else:
return year1, month1 +1 , 1
else:
if day1 < daysOfMonths[month1-1]:
return year1, month1, day1+1
else:
if month1 ==12:
return year1+1,1,1
else:
return year1, month1 +1 , 1
def daysBetweenDates(y1, m1, d1, y2, m2, d2,end_day):
if y1 > y2:
m1,m2 = m2,m1
y1,y2 = y2,y1
d1,d2 = d2,d1
days=0
while(not(m1==m2 and y1==y2 and d1==d2)):
y1,m1,d1 = Count_Days(y1,m1,d1)
days+=1
if end_day:
days+=1
return days
# Test Case
def test():
test_cases = [((2012,1,1,2012,2,28,False), 58),
((2012,1,1,2012,3,1,False), 60),
((2011,6,30,2012,6,30,False), 366),
((2011,1,1,2012,8,8,False), 585 ),
((1994,5,15,2019,8,31,False), 9239),
((1999,3,24,2018,2,4,False), 6892),
((1999,6,24,2018,8,4,False),6981),
((1995,5,24,2018,12,15,False),8606),
((1994,8,24,2019,12,15,True),9245),
((2019,12,15,1994,8,24,True),9245),
((2019,5,15,1994,10,24,True),8970),
((1994,11,24,2019,8,15,True),9031)]
for (args, answer) in test_cases:
result = daysBetweenDates(*args)
if result != answer:
print "Test with data:", args, "failed"
else:
print "Test case passed!"
test()

For calculating dates and times, there are several options but I will write the simple way:
from datetime import timedelta, datetime, date
import dateutil.relativedelta
# current time
date_and_time = datetime.now()
date_only = date.today()
time_only = datetime.now().time()
# calculate date and time
result = date_and_time - timedelta(hours=26, minutes=25, seconds=10)
# calculate dates: years (-/+)
result = date_only - dateutil.relativedelta.relativedelta(years=10)
# months
result = date_only - dateutil.relativedelta.relativedelta(months=10)
# week
results = date_only - dateutil.relativedelta.relativedelta(weeks=1)
# days
result = date_only - dateutil.relativedelta.relativedelta(days=10)
# calculate time
result = date_and_time - timedelta(hours=26, minutes=25, seconds=10)
result.time()
Hope it helps

There is also a datetime.toordinal() method that was not mentioned yet:
import datetime
print(datetime.date(2008,9,26).toordinal() - datetime.date(2008,8,18).toordinal()) # 39
https://docs.python.org/3/library/datetime.html#datetime.date.toordinal
date.toordinal()
Return the proleptic Gregorian ordinal of the date, where January 1 of year 1 has ordinal 1. For any date object d,
date.fromordinal(d.toordinal()) == d.
Seems well suited for calculating days difference, though not as readable as timedelta.days.

from datetime import date
def d(s):
[month, day, year] = map(int, s.split('/'))
return date(year, month, day)
def days(start, end):
return (d(end) - d(start)).days
print days('8/18/2008', '9/26/2008')
This assumes, of course, that you've already verified that your dates are in the format r'\d+/\d+/\d+'.

Here are three ways to go with this problem :
from datetime import datetime
Now = datetime.now()
StartDate = datetime.strptime(str(Now.year) +'-01-01', '%Y-%m-%d')
NumberOfDays = (Now - StartDate)
print(NumberOfDays.days) # Starts at 0
print(datetime.now().timetuple().tm_yday) # Starts at 1
print(Now.strftime('%j')) # Starts at 1

If you want to code the calculation yourself, then here is a function that will return the ordinal for a given year, month and day:
def ordinal(year, month, day):
return ((year-1)*365 + (year-1)//4 - (year-1)//100 + (year-1)//400
+ [ 0,31,59,90,120,151,181,212,243,273,304,334][month - 1]
+ day
+ int(((year%4==0 and year%100!=0) or year%400==0) and month > 2))
This function is compatible with the date.toordinal method in the datetime module.
You can get the number of days of difference between two dates as follows:
print(ordinal(2021, 5, 10) - ordinal(2001, 9, 11))

Without using datetime object in python.
# A date has day 'd', month 'm' and year 'y'
class Date:
def __init__(self, d, m, y):
self.d = d
self.m = m
self.y = y
# To store number of days in all months from
# January to Dec.
monthDays = [31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 ]
# This function counts number of leap years
# before the given date
def countLeapYears(d):
years = d.y
# Check if the current year needs to be considered
# for the count of leap years or not
if (d.m <= 2) :
years-= 1
# An year is a leap year if it is a multiple of 4,
# multiple of 400 and not a multiple of 100.
return int(years / 4 - years / 100 + years / 400 )
# This function returns number of days between two
# given dates
def getDifference(dt1, dt2) :
# COUNT TOTAL NUMBER OF DAYS BEFORE FIRST DATE 'dt1'
# initialize count using years and day
n1 = dt1.y * 365 + dt1.d
# Add days for months in given date
for i in range(0, dt1.m - 1) :
n1 += monthDays[i]
# Since every leap year is of 366 days,
# Add a day for every leap year
n1 += countLeapYears(dt1)
# SIMILARLY, COUNT TOTAL NUMBER OF DAYS BEFORE 'dt2'
n2 = dt2.y * 365 + dt2.d
for i in range(0, dt2.m - 1) :
n2 += monthDays[i]
n2 += countLeapYears(dt2)
# return difference between two counts
return (n2 - n1)
# Driver program
dt1 = Date(31, 12, 2018 )
dt2 = Date(1, 1, 2019 )
print(getDifference(dt1, dt2), "days")

If you don't have a date handling library (or you suspect it has bugs in it), here's an abstract algorithm that should be easily translatable into most languages.
Perform the following calculation on each date, and then simply subtract the two results. All quotients and remainders are positive integers.
Step A. Start by identifying the parts of the date as Y (year), M (month) and D (day). These are variables that will change as we go along.
Step B. Subtract 3 from M
(so that January is -2 and December is 9).
Step C. If M is negative, add 12 to M and subtract 1 from the year Y.
(This changes the "start of the year" to 1 March, with months numbered 0 (March) through 11 (February). The reason to do this is so that the "day number within a year" doesn't change between leap years and ordinary years, and so that the "short" month is at the end of the year, so there's no following month needing special treatment.)
Step D.
Divide M by 5 to get a quotient Q₁ and remainder R₁. Add Q₁ × 153 to D. Use R₁ in the next step.
(There are 153 days in every 5 months starting from 1 March.)
Step E. Divide R₁ by 2 to get a quotient Q₂ and ignore the remainder. Add R₁ × 31 - Q₂ to D.
(Within each group of 5 months, there are 61 days in every 2 months, and within that the first of each pair of months is 31 days. It's safe to ignore the fact that Feb is shorter than 30 days because at this point you only care about the day number of 1-Feb, not of 1-Mar the following year.)
Steps D & E combined - alternative method
Before the first use, set L=[0,31,61,92,122,153,184,214,245,275,306,337]
(This is a tabulation of the cumulative number of days in the (adjusted) year before the first day of each month.)
Add L[M] to D.
Step F
Skip this step if you use Julian calendar dates rather than Gregorian calendar dates; the change-over varies between countries, but is taken as 3 Sep 1752 in most English-speaking countries, and 4 Oct 1582 in most of Europe.
You can also skip this step if you're certain that you'll never have to deal with dates outside the range 1-Mar-1900 to 28-Feb-2100, but then you must make the same choice for all dates that you process.
Divide Y by 100 to get a quotient Q₃ and remainder R₃. Divide Q₃ by 4 to get another quotient Q₄ and ignore the remainder. Add Q₄ + 36524 × Q₃ to D.
Assign R₃ to Y.
Step G.
Divide the Y by 4 to get a quotient Q₅ and ignore the remainder. Add Q₅ + 365 × Y to D.
Step H. (Optional)
You can add a constant of your choosing to D, to force a particular date to have a particular day-number.
Do the steps A~G for each date, getting D₁ and D₂.
Step I.
Subtract D₁ from D₂ to get the number of days by which D₂ is after D₁.
Lastly, a comment: exercise extreme caution dealing with dates prior to about 1760, as there was not agreement on which month was the start of the year; many places counted 1 March as the new year.

Related

Determine a date starting from two integer numbers

Determine a date (as day, month, year) starting from two integer numbers that represent the year and the number of the day in that year.
i'm new to coding and I don't know where to start even.
If I understand correctly you can use Python datetime module to do what you need:
from datetime import datetime
print(datetime.strptime("2004-68", "%Y-%j").strftime("%d.%m.%Y"))
Result:
08.03.2004
You can start with creating algorithm in your head, or on paper.
Then you can translate it to python code.
or
you can read documentation about datetime lib in python and try to combine functions to you desired output. https://docs.python.org/3/library/datetime.html
For example:
from datetime import datetime, timedelta
year = 2004
days = 68
date = datetime(year, 1, 1) + timedelta(days=days-1)
# days-1 is needed because we already start with first day in year
print(date)
# 2004-03-08 00:00:00
year=int(input("year="))
days=int(input("number of days="))
day=int
month = days/28 + 1
m = int(month)
if ((year % 400 == 0) or
(year % 100 != 0) and
(year % 4 == 0)) :
day = days % 30
else:
day = days % 30 + 1
print(day,".", m, ".",year)
is this any good? or more preciseley is it corect? like i need this more basic aproach to the problem

Generate last business day of the month variable in Python for US Calendar

I would like to create a new column in my dataframe which is equal to the last business day of the month based on the US calendar, ideally in the format mm/dd/yyyy. My goal is to create this variable per day and set it for every day in the month corresponding to my existing 'DATES' column. For example, for every day in January I want to create a a variable equal to '01/31/2020', in a string format. For everyday in February I want to set a variable equal to '02/28/2020', in string format.
If anyone has ideas would be of great help!
Dataframe example
This works for me
from datetime import date,timedelta
import datetime
import calendar
today=datetime.date.today()
last = today.replace(day=calendar.monthrange(today.year,today.month)[1])
if last.weekday()<5:
print(last)
else:
print(last-timedelta(days=1+last.weekday()-5))
>>> import datetime
>>> def is_weekend(dt):
return dt.weekday() >= 5
>>> def is_holiday(dt):
return False # You'll need to provide your own definition of holidays
>>> def last_workday(year, month, count=1):
while count > 0:
next_month = month + 1
if next_month <= 12:
next_year = year
else:
next_month = 1
next_year = year + 1
dt = datetime.datetime(next_year, next_month, 1) - datetime.timedelta(days=1)
while is_weekend(dt) or is_holiday(dt):
dt -= datetime.timedelta(days=1)
yield dt
year = next_year
month = next_month
count -= 1
>>> for x in last_workday(2020, 1, 12):
print(x.strftime('%m/%d/%Y'))
01/31/2020
02/28/2020
03/31/2020
04/30/2020
05/29/2020
06/30/2020
07/31/2020
08/31/2020
09/30/2020
10/30/2020
11/30/2020
12/31/2020

Is there a simple way to get the number of years and days between 2 dates?

I have a string representing a birth date I want to know how old the person is in years and days.
e.g: birth = 07/04/1994 (month/days/years)
So the person is 25y 78d old
But so far, using datetime when I do substraction between dates the result it as number of days. And because of leap years I can never be sure of the exact number of days.
In [1]:
from datetime import date, datetime
today = date.today()
birth = '10/21/1996'
birth_date = datetime.strptime(datetime.strptime(birth, '%m/%d/%Y').strftime('%Y-%m-%d'),'%Y-%m-%d').date()
delta = today - birth_date
print(delta.days)
Out [1]:
8369
Not as simple as a built-in Python function, but certainly doable using the following logic:
Construct a date based on the current year minus one, but the birth month and day. Call this lastYearBirthday. Also construct a date based on the current year but the birth month and day. Call this currYearBirthday.
If currYearBirthday is after today (the birthday is yet to occur this year), the number of full years can be obtained by subtracting year(birthdate) from year(lastYearBirthday). The number of days over and above (days since last birthday) that is obtained with (today - lastYearBirthday).days.
Otherwise this years birthday has already happened (or is happening today) and the number of full years can therefore be obtained by subtracting year(birthdate) from year(currYearBirthday) - the number of days over that is obtained with (today - currYearBirthday).days.
Turning that into a Python function you can use easily, we get:
from datetime import date
# Functions to return tuple of (fullYears, extraDays) for
# a given birth date.
def ageInYearsAndDays(birthDate):
# Create relevant dates to ease task.
today = date.today()
lastYearBirthday = date(today.year - 1, birthDate.month, birthDate.day)
currYearBirthday = date(today.year, birthDate.month, birthDate.day)
# Work out years and days based on whether this years
# birthday has happened. Basic idea is that years can
# be calculated as difference between birth year and
# year of most recent birthday. Days is the number of
# days between most recent birthday and today.
if currYearBirthday > today:
years = lastYearBirthday.year - birthDate.year
days = (today - lastYearBirthday).days
else:
years = currYearBirthday.year - birthDate.year
days = (today - currYearBirthday).days
return (years, days)
And some test code shows my own precarious position on this mortal coil:
(years, days) = ageInYearsAndDays(date(1965, 2, 2))
print(years, "years and", days, "days")
The output being (on the day this answer was posted) a rather depressing:
54 years and 230 days
:-)
Note that I just constructed my birthdate from year, month and day directly. Since you already know how to turn a string into one of those (as per your question), I didn't bother using that method.
A user built this following code for another answer on stack that captures the leap year logic. With that said, you will need to refactor to fit your needs...
#Calculate the Days between Two Date
daysOfMonths = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
def isLeapYear(year):
# Pseudo code for this algorithm is found at
# http://en.wikipedia.org/wiki/Leap_year#Algorithm
## if (year is not divisible by 4) then (it is a common Year)
#else if (year is not divisable by 100) then (ut us a leap year)
#else if (year is not disible by 400) then (it is a common year)
#else(it is aleap year)
return (year % 4 == 0 and year % 100 != 0) or year % 400 == 0
def Count_Days(year1, month1, day1):
if month1 ==2:
if isLeapYear(year1):
if day1 < daysOfMonths[month1-1]+1:
return year1, month1, day1+1
else:
if month1 ==12:
return year1+1,1,1
else:
return year1, month1 +1 , 1
else:
if day1 < daysOfMonths[month1-1]:
return year1, month1, day1+1
else:
if month1 ==12:
return year1+1,1,1
else:
return year1, month1 +1 , 1
else:
if day1 < daysOfMonths[month1-1]:
return year1, month1, day1+1
else:
if month1 ==12:
return year1+1,1,1
else:
return year1, month1 +1 , 1
def daysBetweenDates(y1, m1, d1, y2, m2, d2,end_day):
if y1 > y2:
m1,m2 = m2,m1
y1,y2 = y2,y1
d1,d2 = d2,d1
days=0
while(not(m1==m2 and y1==y2 and d1==d2)):
y1,m1,d1 = Count_Days(y1,m1,d1)
days+=1
if end_day:
days+=1
return days
# Test Case
def test():
test_cases = [((2012,1,1,2012,2,28,False), 58),
((2012,1,1,2012,3,1,False), 60),
((2011,6,30,2012,6,30,False), 366),
((2011,1,1,2012,8,8,False), 585 ),
((1994,5,15,2019,8,31,False), 9239),
((1999,3,24,2018,2,4,False), 6892),
((1999,6,24,2018,8,4,False),6981),
((1995,5,24,2018,12,15,False),8606),
((1994,8,24,2019,12,15,True),9245),
((2019,12,15,1994,8,24,True),9245),
((2019,5,15,1994,10,24,True),8970),
((1994,11,24,2019,8,15,True),9031)]
for (args, answer) in test_cases:
result = daysBetweenDates(*args)
if result != answer:
print "Test with data:", args, "failed"
else:
print "Test case passed!"
test()
How to calculate number of days between two given dates?
A lot of exercises from the good old days on stack overflow in September. This should illustrate you the logic. Note that a year is leap only when is perfectly divided by 4, 100 or 400. You can then make use of datetime attributes to have fun.
from datetime import date, datetime
today = date.today()
birth = '10/21/1996'
birth_date = datetime.strptime(datetime.strptime(
birth, '%m/%d/%Y').strftime('%Y-%m-%d'), '%Y-%m-%d').date()
delta = today - birth_date
days = delta.days
year_counter = 0
if today.day >= birth_date.day and today.month >= birth_date.month:
full_years = today.year
else:
full_years = today.year - 1
for year in range(1996, full_years):
if (year % 4) == 0 or (year % 100) == 0 or (year % 400) == 0:
days -= 366
year_counter += 1
else:
days -= 365
year_counter += 1
print("years: " + str(year_counter) + "\ndays: " + str(days))
Obviously there are more Pythonic way to write it, but I suppose you wanted some readability.

Can I make any format mask I like for datetime Python

Given a date and time in a specific format can I use datetime with a format mask that suits to read in the values, when the values aren't all that typical? For example how would I create a mask that works with the following:
08264.51782528
08 = last 2 digits of year (will be within last 60 years so if the 2 digits are above current (eg. 18, then assume they're in the 20th century)
264 = number of days
51782528 = decimal representation of how far through the day (0 = midnight, 0.5 = noon, 0.999988 = 1 second to midnight the following day)
Look into timedelta.
from datetime import datetime, timedelta
# get year, day, and day_percent
if year >= 58:
year += 1900
else:
year += 2000
date = datetime(year, 1, 1) + timedelta(days=day-1) + (timedelta(days=1) * day_percent)
This assumes day 1 is the first day of the year (January 1st).

How to calculate months left to particular date? [duplicate]

This question already has answers here:
Best way to find the months between two dates
(41 answers)
Closed 8 years ago.
In my django app, I have some date.
I need to count how many months left to this date, using full (rounded) months.
eg: today is 19/02/2015 (february), my "search" date is 04/08/2015. Difference should be 6.
How can I get a proper value?
from datetime import datetime,timedelta
from calendar import monthrange
today = datetime.today()
dt = "04/08/2015"
fut = datetime.strptime(dt, "%d/%m/%Y")
diff = 0
while today <= fut:
today += timedelta(days=monthrange(today.day,today.month)[1])
diff += 1
print(diff)
6
Without importing calender we can increment a count everytime we see a new month:
from datetime import datetime,timedelta
today = datetime.today()
dt = "09/08/2015"
fut = datetime.strptime(dt, "%d/%m/%Y")
diff = 0
while today <= fut:
mon = today.month
today += timedelta(days=1)
if today.month != mon:
diff += 1
print(diff)
6
If you want to make the future day the last day of the month:
from datetime import datetime, timedelta
from calendar import monthrange
today = datetime.today()
dt = "02/08/2015"
fut = datetime.strptime(dt, "%d/%m/%Y")
fut = fut + timedelta(days=monthrange(fut.day,fut.month)[1]-fut.day)
diff = 0
while today < fut:
mon = today.month
today += timedelta(days=1)
if today.month != mon:
diff += 1
print(diff)
This is purposely inaccurate to allow for rounding as required, all we care about are the amount of different months we encounter.
I like the arrow library: http://crsmithdev.com/arrow/
eg.
d1 = arrow.get("19/02/2015", "DD/MM/YYYY")
d2 = arrow.get("04/08/2015", "DD/MM/YYYY")
(d2-d1).days
You are going to have decide how to do your calculation. Divide by 30 or extract the months and subtract those.
d2.month - d1.month
To handle it going over a year:
((d2.year * 100) + d2.month) - ((d1.year * 100) + d1.month)
To calculate the month difference (rounded) I would go this direction:
Get the date objects for the different dates (see datetime package). This is rather easy, since the constructor takes year, month, day
Calculate the difference between the dates "date2 - date1" this automatically gives a timedelta object
Get the difference seconds between the two dates by calling "total_seconds()" on the timedelta object
Dividing the number of seconds by 24*60*60 will give the number of days
Dividing the number of days by 30 or (as you like) 31 will give the number of months. You can round the value as you like.
This should suffice:
d,m,y = date1.split('/')
d1 = datetime.date(y, m, d)
d,m,y = date1.split('/')
d2 = datetime.date(y, m, d)
delta = d2 - d1
days = delta.total_seconds() // (24*60*60)
result = int(days/30.0+0.5)
The nice thing: No additional packages needed, all is in the standard packages.

Categories

Resources