1
from datetime import date
import random    
earlier_date = date(2017, 6, random.randint(1, 25))    
later_date = date(2017, 6, random.randint(earlier_date.day + 1, 28))    
days_between = (later_date - earlier_date)    
print("There are",days_between,"days between", earlier_date, "and", later_date)

The output i have (full difference)

There are 18 days, 0:00:00 days between 2017-06-01 and 2017-06-19

The Output I want (day difference only)

There are 3 days between 2017-06-22 and 2017-06-25
8
  • 1
    Duplicates with stackoverflow.com/questions/8258432/days-between-two-dates Commented Jun 28, 2020 at 9:04
  • What is the problem ? You choose random dates, how could you expect a specifif result ? Commented Jun 28, 2020 at 9:04
  • Your code works perfectly for me, it always pick 2 dates, and show the day differences Commented Jun 28, 2020 at 9:05
  • There are 18 days, 0:00:00 days between 2017-06-01 and 2017-06-19 this is the output i am getting ...i want output as i mentioned in question Commented Jun 28, 2020 at 9:07
  • You may have told us in the post at first time. Because I get exact number of date personnally Commented Jun 28, 2020 at 9:09

1 Answer 1

2

Use days_between.days to the days difference. I've also add a s-check for day grammar

# String parts
print("There are", days_between.days, "day" + ('s' if days_between.days > 1 else '') +
      " between", earlier_date, "and", later_date)

# f-string
print(f"There are {days_between.days} day{'s' if days_between.days > 1 else ''} "
      f"between {earlier_date} and {later_date}")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.