Skip to content

Instantly share code, notes, and snippets.

@kuntalchandra
Created September 2, 2020 07:32
Show Gist options
  • Save kuntalchandra/96076366c2baed7e6dba611b69acc543 to your computer and use it in GitHub Desktop.
Save kuntalchandra/96076366c2baed7e6dba611b69acc543 to your computer and use it in GitHub Desktop.
Find the difference between two dates
"""
Write a program to count the number of days between two dates.
The two dates are given as strings, their format is YYYY-MM-DD as shown in the examples.
Example 1:
Input: date1 = "2019-06-29", date2 = "2019-06-30"
Output: 1
Example 2:
Input: date1 = "2020-01-15", date2 = "2019-12-31"
Output: 15
"""
from datetime import datetime
class Solution:
def daysBetweenDates(self, date1: str, date2: str) -> int:
if not date1 or not date2:
return 0
date_1 = datetime.strptime(date1, "%Y-%m-%d")
date_2 = datetime.strptime(date2, "%Y-%m-%d")
return (date_1 - date_2).days if date_1 > date_2 else (date_2 - date_1).days
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment