在 Python 中处理闰年

发布于 2024-10-21 21:22:16 字数 530 浏览 2 评论 0原文

我有一个程序,用户输入一个日期,然后将其与另一个日期进行比较,看看哪个日期先出现。

我该如何编写一段代码,让用户输入 2 月 29 日,程序返回 2 月 28 日(因为没有闰年)?

例子:

def date(prompt):
''' returns the date that user inputs and validates it'''  
while True:
    try:
        date = raw_input(prompt)
        if len(date) >= 5:
            month = date[0:2]
            day = date[3:5]
        dateObject = datetime.date(2011, int(month), int(day))
        return dateObject
    except ValueError:
            print "Please enter a valid month and day"

I have a program where the user enters a date and then compares it to another date to see which one comes first.

How would I go about writing a code where the user inputs Feb 29 and the program returns Feb 28 instead (since there is no leap year)?

Example:

def date(prompt):
''' returns the date that user inputs and validates it'''  
while True:
    try:
        date = raw_input(prompt)
        if len(date) >= 5:
            month = date[0:2]
            day = date[3:5]
        dateObject = datetime.date(2011, int(month), int(day))
        return dateObject
    except ValueError:
            print "Please enter a valid month and day"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愁杀 2024-10-28 21:22:16

您如何比较日期?如果您使用 datetime 函数,那么这应该已经考虑到了这种东西。

>>> datetime.datetime(2011, 2, 28)
datetime.datetime(2011, 2, 28, 0, 0)

>>> datetime.datetime(2011, 2, 29)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: day is out of range for month

>>> datetime.datetime(1600, 2, 29)
datetime.datetime(1600, 2, 29, 0, 0)

datetime.timedelta() 用于表示两个日期之间的差异

>>> datetime.datetime(2011, 2, 28) + datetime.timedelta(days=10)
datetime.datetime(2011, 3, 10, 0, 0)

>>> datetime.datetime(1600, 2, 28) + datetime.timedelta(days=10)
datetime.datetime(1600, 3, 9, 0, 0)

>>> datetime.datetime(2011, 2, 28) - datetime.datetime(2011, 4, 10)
datetime.timedelta(-41)

不知道这如何适合您的代码,但它可能是一个选项;-)

How do you compare dates? If you use the datetime functions then this should already account for this sort of stuff.

>>> datetime.datetime(2011, 2, 28)
datetime.datetime(2011, 2, 28, 0, 0)

>>> datetime.datetime(2011, 2, 29)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: day is out of range for month

>>> datetime.datetime(1600, 2, 29)
datetime.datetime(1600, 2, 29, 0, 0)

datetime.timedelta() is used to represent the difference between two dates

>>> datetime.datetime(2011, 2, 28) + datetime.timedelta(days=10)
datetime.datetime(2011, 3, 10, 0, 0)

>>> datetime.datetime(1600, 2, 28) + datetime.timedelta(days=10)
datetime.datetime(1600, 3, 9, 0, 0)

>>> datetime.datetime(2011, 2, 28) - datetime.datetime(2011, 4, 10)
datetime.timedelta(-41)

Don't know how this fits in your code, but it may be an option ;-)

jJeQQOZ5 2024-10-28 21:22:16

如果您要检查:month == 2 and day == 29,那么使用 calendar.isleap()。如果将 date/datetime 设置为非闰年的 2 月 29 日,则会引发 ValueError

If you are checking that: month == 2 and day == 29 it is redundant to also check if it is a leap year with calendar.isleap(). A date/datetime would raise a ValueError if it was set to Feb 29th on a non-leapyear.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文