[英]finding first day of the month in python
我试图在 python 中找到该月的第一天,但有一个条件:如果我当前的日期超过了该月的 25 日,那么第一个日期变量将保存下个月的第一个日期而不是当前月份。 我正在执行以下操作:
import datetime
todayDate = datetime.date.today()
if (todayDate - todayDate.replace(day=1)).days > 25:
x= todayDate + datetime.timedelta(30)
x.replace(day=1)
print x
else:
print todayDate.replace(day=1)
有没有更清洁的方法来做到这一点?
可以使用date.replace
在同一行上date.replace
:
from datetime import datetime
datetime.today().replace(day=1)
这是一个精辟的解决方案。
import datetime
todayDate = datetime.date.today()
if todayDate.day > 25:
todayDate += datetime.timedelta(7)
print todayDate.replace(day=1)
原始代码示例需要注意的一点是,如果您在 1 月的最后一天进行测试,则使用timedelta(30)
会导致问题。 这就是我使用 7 天增量的原因。
使用dateutil 。
from datetime import date
from dateutil.relativedelta import relativedelta
today = date.today()
first_day = today.replace(day=1)
if today.day > 25:
print(first_day + relativedelta(months=1))
else:
print(first_day)
from datetime import datetime
date_today = datetime.now()
month_first_day = date_today.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
print(month_first_day)
使用箭头。
import arrow
arrow.utcnow().span('month')[0]
是的,首先将日期时间设置为当月的开始。
第二次测试当前日期天 > 25 并得到一个真/假。 如果为 True,则在月初日期时间对象中添加一个月。 如果为 false,则使用 datetime 对象,其值设置为月初。
import datetime
from dateutil.relativedelta import relativedelta
todayDate = datetime.date.today()
resultDate = todayDate.replace(day=1)
if ((todayDate - resultDate).days > 25):
resultDate = resultDate + relativedelta(months=1)
print resultDate
箭头模块将引导您避开细微的错误,并且更容易使用旧产品。
import arrow
def cleanWay(oneDate):
if currentDate.date().day > 25:
return currentDate.replace(months=+1,day=1)
else:
return currentDate.replace(day=1)
currentDate = arrow.get('25-Feb-2017', 'DD-MMM-YYYY')
print (currentDate.format('DD-MMM-YYYY'), cleanWay(currentDate).format('DD-MMM-YYYY'))
currentDate = arrow.get('28-Feb-2017', 'DD-MMM-YYYY')
print (currentDate.format('DD-MMM-YYYY'), cleanWay(currentDate).format('DD-MMM-YYYY'))
例如,在这种情况下,您无需考虑不同的月份长度。 这是此脚本的输出。
25-Feb-2017 01-Feb-2017
28-Feb-2017 01-Mar-2017
这可能是古斯塔沃·爱德华多·贝尔杜马 (Gustavo Eduardo Belduma) 回答的替代方案:
import datetime
first_day_of_the_month = datetime.date.today().replace(day=1)
您可以使用dateutil.rrule :
In [1]: from dateutil.rrule import *
In [2]: rrule(DAILY, bymonthday=1)[0].date()
Out[2]: datetime.date(2018, 10, 1)
In [3]: rrule(DAILY, bymonthday=1)[1].date()
Out[3]: datetime.date(2018, 11, 1)
我找到当月的第一天和最后一天的解决方案:
def find_current_month_last_day(today: datetime) -> datetime:
if today.month == 2:
return today.replace(day=28)
if today.month in [4, 6, 9, 11]:
return today.replace(day=30)
return today.replace(day=31)
def current_month_first_and_last_days() -> tuple:
today = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
first_date = today.replace(day=1)
last_date = find_current_month_last_day(today)
return first_date, last_date
下个月的第一天:
from datetime import datetime
class SomeClassName(models.Model):
if datetime.now().month == 12:
new_start_month = 1
else:
new_start_month = datetime.now().month + 1
然后我们替换月和日
start_date = models.DateField(default=datetime.today().replace(month=new_start_month, day=1, hour=0, minute=0, second=0, microsecond=0))
我发现一个干净的方法是使用 todayDate 的月份和年份属性创建一个日期时间对象,天数设置为 1 即
import datetime
todayDate = datetime.date.today()
firstOfMon = datetime.date(todayDate.year, todayDate.month, 1)
单线:
from datetime import datetime, timedelta
last_month=(datetime.now().replace(day=1) - timedelta(days=1)).replace(day=1)
受Jouberto和@akx的答案(其他地方)的启发,没有任何依赖关系的 oneliners:
now = datetime.datetime.now(tz=ZoneInfo("UTC"))
this_month = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
next_month = (this_month.replace(day=28) + datetime.timedelta(days=4)).replace(day=1)
last_month = (this_month.replace(day=1) - datetime.timedelta(days=1)).replace(day=1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.