gpt4 book ai didi

python-2.7 - 如何知道当前月份属于哪个季度? (在 python 中)

转载 作者:行者123 更新时间:2023-12-04 16:49:35 25 4
gpt4 key购买 nike

我想知道当前月份在python中属于哪个季度(Q1,Q2,Q3,Q4)。我通过导入时间模块获取当前日期,如下所示:

import time
print "Current date " + time.strftime("%x")

知道怎么做吗?

最佳答案

修改你的代码,我明白了:

import time

month = int(time.strftime("%m")) - 1 # minus one, so month starts at 0 (0 to 11)
quarter = month / 3 + 1 # add one, so quarter starts at 1 (1 to 4)
quarter_str = "Q" + str(quarter) # convert to the "Qx" format string
print quarter_str

或者你可以使用 bisect 模块:

import time
import bisect

quarters = range(1, 12, 3) # This defines quarters: Q1 as 1, 2, 3, and so on
month = int(time.strftime("%m"))

quarter = bisect.bisect(quarters, month)
quarter_str = = "Q" + str(quarter)
print quarter_str

关于python-2.7 - 如何知道当前月份属于哪个季度? (在 python 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25801630/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com