【问题标题】:How to check given date is exactly a month ago : python如何检查给定日期恰好是一个月前:python
【发布时间】:2021-12-15 23:55:43
【问题描述】:

我需要检查给定日期是否正好是从今天开始的一个月前,例如,如果今天是 2021 年 11 月 1 日,那么正好一个月前就是 2021 年 10 月 1 日(不完全是 30 天。)

我写了一段代码,效果很好

today = fields.Date.from_string(fields.Date.today())
if today.month == 1:
    one_month_ago = today.replace(year=today.year - 1, month=12)
else:
    extra_days = 0
    while True:
        try:
            one_month_ago = today.replace(month=today.month - 1, day=today.day - 
                      extra_days)
            break
        except ValueError:
            extra_days += 1
 


if one_month_ago == given_date:
    # do something
else:
    # do something

它可以很好地处理所有情况,但处理不当。例如,给定的日期是 2021 年 3 月 31 日,今天的日期是 2021 年 4 月 30 日,而 2021 年 4 月 31 日将不会来比较。我需要我的代码每天运行并检查一些东西。它还错误地处理了 1 月 29 日至 31 日的案例,因为 2 月 29 日至 31 日的情况无法进行比较。

【问题讨论】:

  • 您必须添加一些逻辑来处理前一个月天数较少的所有月份,这显然取决于您决定如何处理它们。你想在 3 月 31 日 29 日/30 日发生什么,因为没有 2 月 30 日/31 日,而且大多数年份都没有 2 月 29 日。
  • 什么是“一个月前”? 12月31日? 3月29日?闰年的 3 月 29 日?
  • @Grismar 恰好一个月前的 12 月 31 日将是 11 月 30 日,3 月 29 日将是 2 月 28 日,而在闰年,它将是 2 月 29 日。
  • @balmy 这正是我想要处理的。我知道所有可能发生的缺失日期,我不想单独处理每个案例,因为我仍然会错过一些东西。即使一个案例处理不当,也可能导致问题。
  • 所以你有了逻辑,现在将它添加到你的代码中。或者你希望有人会为你写吗?如果是这样,请注意 StackOverflow 不是 代码编写服务。您已经在处理前一年的 1 月 - 继续编写类似的测试和调整,为您需要调整的几个月。

标签: python python-3.x datetime python-datetime


【解决方案1】:

从给定的日期,您可以找到上个月和上一年,并使用这两个获得的值,找到上个月的长度。最后要做的是将给定日期的日期与上个月的长度进行比较,并相应地返回所需的日期。

演示:

from datetime import date
from calendar import monthrange


def date_a_month_ago(today):
    x = today.month - 1
    previous_month = 12 if x == 0 else x
    year = today.year - 1 if x == 0 else today.year
    last_day_of_previous_month = monthrange(year, previous_month)[1]
    day = last_day_of_previous_month if today.day > last_day_of_previous_month else today.day
    return date(year, previous_month, day)


# Tests
print(date_a_month_ago(date(2021, 11, 1)))
print(date_a_month_ago(date(2021, 1, 31)))
print(date_a_month_ago(date(2021, 12, 31)))
print(date_a_month_ago(date(2021, 3, 29)))
print(date_a_month_ago(date(2020, 3, 29)))
print(date_a_month_ago(date(2021, 3, 30)))
print(date_a_month_ago(date(2020, 3, 30)))

输出:

2021-10-01
2020-12-31
2021-11-30
2021-02-28
2020-02-29
2021-02-28
2020-02-29

ONLINE DEMO

【讨论】:

  • 不完全是我的解决方案,但帮助很大。
【解决方案2】:

可能是这样的:

from typing import Tuple

def last_month(year: int, month: int) -> Tuple[int, int]:
    y, m = year, month - 1
    if m == 0:
        y, m = y - 1, 12
    return y, m

def one_month_ago(today: datetime) -> datetime:
    y, m = last_month(today.year, today.month)
    dt = today.replace(year=y, month=m)
    for day in range(today.day, 0, -1):
        try:
            return dt.replace(day=day)
        except ValueError:
            ...

【讨论】:

    【解决方案3】:

    我这样做了,它给了我所需的输出:

    from datetime import date
    from calendar import monthrange
    
    
    def is_one_month(given_date, today):
        x = today.month - 1
        previous_month = 12 if x == 0 else x
        year = today.year - 1 if x == 0 else today.year
        last_day_of_previous_month = monthrange(year, previous_month)[1]
        day = last_day_of_previous_month if today.day > last_day_of_previous_month else today.day
        one_month_ago = date(year, previous_month, day)
        if today.month == 2:
            if given_date.month == today.month-1 and given_date.year == today.year and given_date.day >= 28:
                return 'it is one month before'
        if today.month == 4 or today.month == 6 or today.month == 9 or today.month == 11:
            if given_date.month == today.month-1 and given_date.day == 31:
                return 'it is one month before'
        if one_month_ago == given_date:
            return 'it is one month before'
        else:
            return 'it is NOT one month before'
    
    print(is_one_month(date(2021, 1, 30), date(2021, 2, 28)))
    

    输出:

    it is one month before
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-13
      • 1970-01-01
      • 2014-01-20
      相关资源
      最近更新 更多