Q:

Calculate number of days between two given dates using python programming

belongs to collection: Python Date and Time Exercises

0

Calculate number of days between two given dates

Given:

# 2020-02-25
date_1 = datetime(2020, 2, 25)

# 2020-09-17
date_2 = datetime(2020, 9, 17)

Expected output:

205 days

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

from datetime import datetime

# 2020-02-25
date_1 = datetime(2020, 2, 25).date()
# 2020-09-17
date_2 = datetime(2020, 9, 17).date()

delta = None
if date_1 > date_2:
    print("date_1 is greater")
    delta = date_1 - date_2
else:
    print("date_2 is greater")
    delta = date_2 - date_1
print("Difference is", delta.days, "days")

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

<< Calculate the date 4 months from the current date ...