Python Forum
count of days between two dates
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
count of days between two dates
#1
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.
Reply
#2
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
Reply
#3
i don't wanna use any static data.
is there any solution to use dynamic variables?
the result are depending on user's input.
Reply
#4
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
Reply
#5
thanks a lot!
i saw this example. but couldn't apply for my program.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Row Count and coloumn count Yegor123 4 1,268 Oct-18-2022, 03:52 AM
Last Post: Yegor123
  How split N days between specified start & end days SriRajesh 2 1,303 May-06-2022, 02:12 PM
Last Post: SriRajesh
  How can I count values between range dates ? Eidrizi 2 2,412 Mar-17-2021, 01:26 PM
Last Post: Eidrizi
Brick How To Sum Numbers Of Next 7 Days Developer_2018 7 2,678 Oct-19-2020, 02:54 PM
Last Post: Developer_2018
  Make list of dates between today back to n days Mekala 3 2,339 Oct-03-2020, 01:01 PM
Last Post: ibreeden
  How many money in 30 days. miguelramos122 4 5,678 Dec-16-2017, 12:48 PM
Last Post: squenson

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020