如何检查给定日期恰好是一个月前:python

How to check given date is exactly a month ago : python

我需要检查给定日期是否恰好是从今天算起的一个月前,例如,如果今天是 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

它处理了大部分情况,但处理了一些情况。例如,给定的日期是 31-March-2021 而今天的日期是 30-April-2021 并且 31-April-2021 不会来比较。我需要我的代码每天 运行 并检查一些东西。 1月29-31日的案例也处理不当,因为2月29-31日不会来比较。

可能是这样的:

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:
            ...

从给定的日期开始,您可以找到前一个月和前一年,并使用这两个获得的值,找到前一个月的长度。最后要做的是将给定日期的日期与上个月的长度进行比较,因此 return 所需的日期。

演示:

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

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

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