Code Coach Solutions
Days Between Dates[Pro]
Difficulty: Medium
Problem Statement:
You need to calculate exactly how many days have passed between the two dates.
Task:
Calculate how many days have passed between two input dates, and output the result.
Input Format:
Two strings represent the dates, the first date should be the older date.
Date format: Month DD, YYYY
Output Format:
A number representing the number of days between the two dates.
Sample Input:
August 15, 1979
June 15, 2018
Sample Output:
14184
Explanation:
14184 days have passed between the two input days.
Solution:
from datetime import date
x=input().split(',')
y=input().split(',')
x1=x[0].split()
y1=y[0].split()
print(x1)
print(y1)
l=['January','February','March','April','May','June','July','August','September','October','November','December']
d1=date(int(x[1]),int(l.index(x1[0])),int(x1[1]))
d2=date(int(y[1]),int(l.index(y1[0])),int(y1[1]))
print((d2-d1).days)
Explanation:
The main objective of this problem is to find the no of days between the given dates. In python, we have date() method to pass date as an argument. By subtracting the present date with the previous date we will get the output in date format. So, to convert into days use .days. The syntax of date method is date(year, month, date).
Result:
Hope You understand the solution. The more you practice more you Learn. If there are any queries let us know in the comment section.
Tips to solve a problem in less time:
1. Don't read the whole problem statement initially. First, go through the input and output.


0 Comments