获取Python当前季度的第一个日期和最后一个日期?

Kri*_*shh 1 python-2.7

如何在Python中获取当前季度以及当前季度的第一个日期和最后一个日期?

我想通过导入日期时间

import datetime
Run Code Online (Sandbox Code Playgroud)

人们认为堆栈溢出需要直接答案,这应该非常简单。您提供的哪个链接都包含很多评论。因此,用户必须浏览所有评论才能找到正确的答案。我正在写简单明了的答案。

Pet*_*ter 13

我相信当前的答案在 Python 3 中都仍然有效,因此由于这是 google 季度第一天和最后一天的热门话题,我将提供一个适用于 Python 3 的解决方案(主要是 Ahmet 使用 // 而不是/):

from datetime import date as date_class
from datetime import timedelta, datetime


def get_quarter(p_date: date_class) -> int:
    return (p_date.month - 1) // 3 + 1


def get_first_day_of_the_quarter(p_date: date_class):
    return datetime(p_date.year, 3 * ((p_date.month - 1) // 3) + 1, 1)


def get_last_day_of_the_quarter(p_date: date_class):
    quarter = get_quarter(p_date)
    return datetime(p_date.year + 3 * quarter // 12, 3 * quarter % 12 + 1, 1) + timedelta(days=-1)


assert get_quarter(datetime(year=2021, month=10, day=5).date()) == 4
assert get_quarter(datetime(year=2020, month=9, day=25).date()) == 3
assert get_quarter(datetime(year=2020, month=12, day=11).date()) == 4
assert get_quarter(datetime(year=2020, month=1, day=2).date()) == 1

assert get_first_day_of_the_quarter(datetime(2020, 10, 5).date()) == datetime(2020, 10, 1)
assert get_first_day_of_the_quarter(datetime(2020, 9, 25).date()) == datetime(2020, 7, 1)
assert get_first_day_of_the_quarter(datetime(2020, 12, 11).date()) == datetime(2020, 10, 1)
assert get_first_day_of_the_quarter(datetime(2020, 1, 2).date()) == datetime(2020, 1, 1)

assert get_last_day_of_the_quarter(datetime(2020, 10, 5).date()) == datetime(2020, 12, 31)
assert get_last_day_of_the_quarter(datetime(2020, 9, 25).date()) == datetime(2020, 9, 30)
assert get_last_day_of_the_quarter(datetime(2020, 12, 11).date()) == datetime(2020, 12, 31)
assert get_last_day_of_the_quarter(datetime(2020, 1, 2).date()) == datetime(2020, 3, 31)
assert get_last_day_of_the_quarter(datetime(2020, 5, 6).date()) == datetime(2020, 6, 30)
Run Code Online (Sandbox Code Playgroud)

  • get_first_day_of_the_quarter 不起作用,它将季度返回为月份,这是错误的。它应该返回 ((quarter-1) * 3) +1 因此,如果当前月份是 11,则给出 (4-1) * 3 + 1,即 10(这是第四季度开始的正确月份) ) (2认同)

Kri*_*shh 8

我如何在 C# 中找到一些简单的解决方案并将其转换为 python,

from datetime import datetime,timedelta
current_date=datetime.now()
currQuarter = (current_date.month - 1) / 3 + 1
dtFirstDay = datetime(current_date.year, 3 * currQuarter - 2, 1)
dtLastDay = datetime(current_date.year, 3 * currQuarter + 1, 1) + timedelta(days=-1)
Run Code Online (Sandbox Code Playgroud)

  • 当 current_date 位于最后一个季度时,获取“{ValueError}month must be in 1..12”。 (8认同)
  • 我只想建议将 currQuarter 设置为整数。`currQuarter = int((current_date.month - 1) / 3 + 1)` 并且还要避免 CamelCase :D (2认同)

Ahm*_*DAL 6

第一天与@Karishh的解决方案相同。但是,对于最后一个日期,Python2.7导致第四季度出现问题。因为12 + 1 = 13并且datetime不接受13作为一个月。因此,您需要采取一些技巧来处理它。

import datetime


def get_quarter(date):
    return (date.month - 1) / 3 + 1

def get_first_day_of_the_quarter(date):
    quarter = get_quarter(date)
    return datetime.datetime(date.year, 3 * quarter - 2, 1)

def get_last_day_of_the_quarter(date):
    quarter = get_quarter(date)
    month = 3 * quarter
    remaining = month / 12
    return datetime.datetime(date.year + remaining, month % 12 + 1, 1) + datetime.timedelta(days=-1)
Run Code Online (Sandbox Code Playgroud)

  • 这也可能引发TypeError异常。您的函数`get_first_day_of_quarter`会为`quarter`生成一个浮点数,但`datetime.datetime()`不会是浮点数obj。在`get_quarter(date)`周围添加`int()`可能会解决此问题,因为例如您现在仍在第二季度,但您将收到`quarter = 2.66667`。 (2认同)