简体   繁体   English

确定在第15天之后会转到下个月的当前月份变量-python 2.7

[英]Determine current month variable that rolls over to next month after the 15th day- python 2.7

I use the current_month variable to query data but here's the catch- if the day in the month is later then the 15th, I want to set the current month to be the following month. 我使用current_month变量查询数据,但这是捕获的问题-如果该月中的某天晚于15日,则要将当前月份设置为下个月。 So the current month for 4/16/2016 should be 5/1/2016. 因此,2016年4月16日的当前月份应为2016年5月1日。 I've got code that works but it doesn't feel pythonic. 我有能正常工作的代码,但感觉不到pythonic。 Suggestions would be appreciated. 建议将不胜感激。

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")

An alternative to Scott's approach (which works perfectly well and you should probably accept it) is to use a dateutil.relativedelta : 斯科特方法的一种替代方法(效果很好,您可能应该接受它)是使用dateutil.relativedelta

from datetime import datetime
from dateutil.relativedelta import relativedelta

def next_month_after_day(dt, trans_day=15):
    rd = relativedelta(months=(dt.day > trans_day), day=1)

    return dt + rd

dt1 = datetime(1996, 2, 3)
dt2 = datetime(1996, 2, 15)
dt3 = datetime(1996, 2, 16)
dt4 = datetime(1996, 12, 18)

fmt = "%Y-%m-%d"
for x in (dt1, dt2, dt3, dt4):
    dt_before = x
    dt_after = next_month_after_day(x)
    print('{} -> {}'.format(dt_before.strftime(fmt), dt_after.strftime(fmt)))

The result is: 结果是:

1996-02-03 -> 1996-02-01
1996-02-15 -> 1996-02-01
1996-02-16 -> 1996-03-01
1996-12-18 -> 1997-01-01

Thanks to Scott for pointing out why my original approach was needlessly complicated (and wrong). 感谢Scott指出为什么我的原始方法不必要地复杂(和错误)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何线性插补从下个月15日开始的每月15号的每月数据 - how to linearly interpolate monthly data from the 15th of each month that rolls over the 15th of the next month 需要计算从下个月15号到15号的操作总和 - Need to count the total sum of operations from 15th day to 15th day of next month Select 每月的第一天/第十五天加上 python 的前后一天 - Select first/15th day of month plus the day before and after in python Django ORM过滤上个月15日到本月15日之间的记录 - Django ORM filter records between last month 15th date to current month 15th date 计算python中每月的第1个或第15个月的时间 - Calculating time until 1st or 15th of the month in python select 按日期记录,其中日期是该月上一个工作日的 15 日 - select records by date, where date is 15th ot the previous business day of the month Discord.py 每月15号发一个embed - Discord.py Sending an embed on the 15th every month pandas 每月重新抽样第 15 天 - pandas monthly resample 15th day 如何确定每月的第 4 天(不包括星期一和星期日) - How to determine if 4th day of the month (Mon and Sun not included) 是否可以在python中将日+月(不是年)与当前日+月进行比较? - Is it possible to compare day + month(not year) against current day + month in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM