1

from datetime import datetime

x = input("first date: ")

y = input("second date: ")


a = datetime.strptime(x, "%Y/%m/%d")

b = datetime.strptime(y, "%Y/%m/%d")


result = (a-b).days


print("days: ",result)


# my first date is = 2021/2/8

# my second date is = 2021/1/24

# output = days : 15

So as you see everything is fine in this code But my teacher make a challenge for me . He said can you write a code with unusual days in months . For ex : January have 31 days but I want it to be 41 days and etc .

What should I do now ? (Please don't say : sum the output with 10 because the user inputs could be changeable and I should change all of the days in months so this will not work)

I am amatuar in coding so simple explanation would be better.

So I am looking for something like this :

# if January have 41 days instead of 31 days

# my first date is = 2021/2/8

# my second date is = 2021/1/24

# output will be = days : 15 + 10 = 25

The Epic
  • 137
  • 5

2 Answers2

1

You can make dictionary consisting of months and their custom days (For example, '1': 41 means first month consisting of 41 days). Then all you need to do is to add input date of the first month with the subtraction of total days of current month and days of input date. (Assuming first date is always greater than the second).

months = {
'1': 41,
'2': 38,
'3': 24,
...
...
'12': 45,
}

x = input("first date: ")
y = input("second date: ")

a = list(x.split('/'))
b = list(y.split('/'))

# 2021/2/8
# ['2021', '2', '8']

result = int(a[2]) + (months[b[1]] - int(b[2]))
print(result)
Rustam Garayev
  • 2,632
  • 1
  • 9
  • 13
0

I think you're close the answer. you don't want to 'sum the output with 10', but why not? the answer to the problem is 'result + extra_days' (so sum of output + offset). So instead of the '10' you want the offset, the offset is maxDayOfMonth +/- requestedDate

Here is a related post which gives a function to get the last day of any month:

def last_day_of_month(any_day):
    # this will never fail
    # get close to the end of the month for any day, and add 4 days 'over'
    next_month = any_day.replace(day=28) + datetime.timedelta(days=4)
    # subtract the number of remaining 'overage' days to get last day of current month, or said programattically said, the previous day of the first of next month
    return next_month - datetime.timedelta(days=next_month.day)

It always helps to find a scenario for your problem, for example: Your teacher discoverd an alternate universe where the days in a month are variable, and he wants to use the datetime library to work. :)

user_cr
  • 110
  • 10
  • some programmers might die from immediate brain cancer in that universe ^^ seriously: the problem I see here is that `any_day` is still a datetime object - which cannot handle dates like 2021-01-41. – FObersteiner Jan 29 '21 at 14:33
  • I could be more clear: use last_day_of_month(any_day) to get the 'normal' last day. Then you can get the offset by subtracting the returnvalue with the 'imaginary' last day. – user_cr Jan 29 '21 at 14:38