简体   繁体   English

计算 python 中月份中具有唯一天数的日期之间的天数

[英]Calculate days between dates with unique days in months in python


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.例如:一月有 31 天,但我希望它是 41 天等等。

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) (请不要说:将 output 与 10 相加,因为用户输入可能是可变的,我应该在几个月内更改所有天数,所以这不起作用)

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

You can make dictionary consisting of months and their custom days (For example, '1': 41 means first month consisting of 41 days).您可以制作由月份及其自定义日期组成的字典(例如,'1':41 表示第一个月由 41 天组成)。 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)

I think you're close the answer.我想你已经接近答案了。 you don't want to 'sum the output with 10', but why not?您不想“将 output 与 10 相加”,但为什么不呢? the answer to the problem is 'result + extra_days' (so sum of output + offset).问题的答案是'result + extra_days'(所以是 output + offset 的总和)。 So instead of the '10' you want the offset, the offset is maxDayOfMonth +/- requestedDate因此,您想要的偏移量不是“10”,而是 maxDayOfMonth +/- requestedDate

Here is a related post which gives a function to get the last day of any month: 是一个相关的帖子,它给出了一个 function 来获取任何一个月的最后一天:

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.为您的问题找到一个场景总是有帮助的,例如:您的老师发现了一个替代宇宙,其中一个月中的日子是可变的,他想使用 datetime 库来工作。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM