Python Forum
count of days between two dates - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: count of days between two dates (/thread-9458.html)



count of days between two dates - ikeen - Apr-10-2018

good day
i try learn and i'm beginner in python.
can somebody help me, please?
i'm writing a little joke program. and one of purpose of these program calculate count of days between two dates. on date is current day (for example, name of variable cd in format "%Y,%m,%d") and other some calculated day in future (name of variable pd in format "%Y,%m,%d").
i try to calculate count between that variables.
part of code:
cd = (cur_date.strftime("%Y,%m,%d"))
pd = d.datetime.strftime(birthday + d.timedelta(days=days_pens + 1), "%Y,%m,%d")
delta_days = d.date(int(pd)) - d.date(int(cd))

part of output and full error:
2045,05,18 (variable pd)
2018,04,10 (variable cd)
Traceback (most recent call last):
File "D:/temp/Python/шаблоны/your_pension_estimated_time.py", line 52, in <module>
delta_days = d.date(int(pd)) - d.date(int(cd))
ValueError: invalid literal for int() with base 10: '2045,05,18'

i apologize that i need use cycle for my purpose like this:
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
.... etc

but i would like to go by a shorter route. tell me please how solve my problem.


RE: count of days between two dates - scidam - Apr-10-2018

This could help you:

import datetime
a = datetime.datetime.strptime("2014,10,10","%Y,%m,%d")
b = datetime.datetime.strptime("2018,10,10","%Y,%m,%d")
(b-a).days
Output:
1461



RE: count of days between two dates - ikeen - Apr-10-2018

i don't wanna use any static data.
is there any solution to use dynamic variables?
the result are depending on user's input.


RE: count of days between two dates - scidam - Apr-10-2018

No problem, try this:
import datetime
x = input("Enter first date in Y,m,d format:")
y = input("Enter second date in Y,m,d format:")
a = datetime.datetime.strptime(x,"%Y,%m,%d")
b = datetime.datetime.strptime(y,"%Y,%m,%d")
(b-a).days



RE: count of days between two dates - ikeen - Apr-10-2018

thanks a lot!
i saw this example. but couldn't apply for my program.