gpt4 book ai didi

python - 确定第 15 天后滚动到下个月的当前月份变量 - python 2.7

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

我使用 current_month 变量来查询数据,但有一个问题 - 如果该月的某一天晚于 15 号,我想将当前月份设置为下个月。因此 4/16/2016 当前月份应为 5/1/2016。我的代码可以工作,但感觉不像Python。如有建议,我们将不胜感激。

month = datetime.datetime.now().strftime("%m")
year = datetime.datetime.now().strftime("%Y")
day = datetime.datetime.now().strftime("%d")

#Check if day in the month is past 15th if so set current month
if int(day) > 15:
if int(month) < 9: # check if the month in 1-9 if so pad leading zero
x = int(month)+1
current_month = year+"-0"+str(x)+"-01"
if int(month) == 9: # check if the month in 1-9 if so pad leading zero
x = int(month)+1
current_month = year+"-"+str(x)+"-01"
elif int(month) == 12: # check if the month is Dec if so roll to the next year and set month to Jan
month = "01"
y = int(year)+1
current_month = str(y)+"-"+month+"-01"
else:
x = int(month)+1 # just add one to the month if months are 10 or 11
current_month = year+"-"+str(x)+"-01"
else:
current_month = year+"-"+month+"-01" #prior to the 15'th so just use normal year, month and day

最佳答案

# Get today's date/time
today = datetime.datetime.now()
# add 16 days if after the 15th
if today.day > 15:
today += datetime.timedelta(16)
# Format that date w/ day being 1
current_month = today.strftime("%Y-%m-01")

关于python - 确定第 15 天后滚动到下个月的当前月份变量 - python 2.7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36658606/

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