简体   繁体   English

获取上个月的第一天 - 月份必须在 1..12

[英]Get first day of previous month - month must be in 1..12

I tried this and I am getting an error month must be in 1..12 :我试过这个,我得到一个错误month must be in 1..12

today_date = date.today()
# ERROR - month value becomes -1 here instead of 12
previous_month_4_start_date = today_date.replace(month=today_date.month - 4, day=1)

I can write some boilerplate code to fix this but looking for an efficient approach where if month is -1 or -2 or -3 or -4 then it should be replaced to 12 or 11 or 10 or 9 respectively.我可以编写一些样板代码来解决这个问题,但寻找一种有效的方法,如果month-1-2-3-4 ,那么它应该分别替换为1211109

Please help请帮忙

EDIT编辑

Thanks for helping with the month, how to take care of the year part since it will also change from 2020 to 2019 ?感谢您对本月的帮助,由于它也会从2020变为2019 ,因此如何照顾年部分? Please help请帮忙

You can use modulo arithmetic for this problem, but first you have to convert the month number into a value which is valid modulo 12 (ie 0-11 ) by subtracting one from it.可以对这个问题使用modulo算术,但首先您必须通过从中减去一个将month数转换为有效模 12 的值(即0-11 )。 You can then subtract n months from that value, take the modulus and convert it back to a month number by adding 1 again:然后,您可以从该值中减去n个月,取模并通过再次加 1 将其转换回month数:

today_date = date.today()

previous_month_4_start_date = today_date.replace(month=(today_date.month - 1 - 4) % 12 + 1, day=1)
print(previous_month_4_start_date)

Output (as of 2020-04-14 ): 2020-04-14 (截至 2020 年 4 月 14 日):

2020-12-01

This doesn't deal with the fact that the year changes, to do that you need to compare the current month with the number being subtracted and if it is more than the current month, also subtract one from the year:这不涉及年份变化的事实,为此您需要将当前月份与被减去的数字进行比较,如果它大于当前月份,还从年份中减去一个:

previous_month_4_start_date = today_date.replace(year=today_date.year-1 if today_date.month <= 4 else today_date.year,
                                                 month=(today_date.month - 1 - 4) % 12 + 1, 
                                                 day=1)

Output: Output:

2019-12-01

But

That's the hard way to do it.这是很难做到的。 The simple solution is to set the day to 1 and then use relativedelta to subtract 4 months:简单的解决方案是将day设置为1 ,然后使用relativedelta减去 4 个月:

from datetime import date
from dateutil.relativedelta import relativedelta

today_date = date.today()

previous_month_4_start_date = today_date.replace(day = 1) - relativedelta(months = 4)
print(previous_month_4_start_date)

Output: Output:

2019-12-01

The modulus operator is your best bet here.模数运算符是您最好的选择。 It does exactly what you need.它完全符合您的需要。 You need to subtract 5 instead of 4 because the number needs to flow into negatives for the code to work:您需要减去 5 而不是 4,因为数字需要变成负数才能使代码正常工作:

previous_month_4_start_date = today_date.replace(month=(today_date.month - 5) % 12 + 1, day=1)
# Returns: 12
#Try this to get the previous month's first day    

from datetime import datetime,date
today_date = date.today()
if today_date.month == 1:
    previous_month_4_start_date = today_date.replace(month=(12), day=1)
else:
    previous_month_4_start_date = today_date.replace(month=(today_date.month-1), day=1)
day_name= ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday','Sunday']
print(day_name[previous_month_4_start_date.weekday()])
print(previous_month_4_start_date)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM