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
azro
  • 53,056
  • 7
  • 34
  • 70
  • 1
    Duplicates with https://stackoverflow.com/questions/8258432/days-between-two-dates – minglyu Jun 28 '20 at 09:04
  • What is the problem ? You choose random dates, how could you expect a specifif result ? – azro Jun 28 '20 at 09:04
  • Your code works perfectly for me, it always pick 2 dates, and show the day differences – azro Jun 28 '20 at 09: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 – D. Suresh Roshi Jun 28 '20 at 09:07
  • You may have told us in the post at first time. Because I get exact number of date personnally – azro Jun 28 '20 at 09:09
  • @azro i dont want specific result.. i want my code to be in a format that i mentioned – D. Suresh Roshi Jun 28 '20 at 09:11
  • Look the edit I've made to your post, to understand how it should be – azro Jun 28 '20 at 09:16
  • Does this answer your question? [How to calculate number of days between two given dates?](https://stackoverflow.com/questions/151199/how-to-calculate-number-of-days-between-two-given-dates) – above_c_level Jun 28 '20 at 11:53

1 Answers1

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}")
azro
  • 53,056
  • 7
  • 34
  • 70