finding first day of the month in python

2020-05-25 03:51发布

I'm trying to find the first day of the month in python with one condition: if my current date passed the 25th of the month, then the first date variable will hold the first date of the next month instead of the current month. I'm doing the following:

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)

is there a cleaner way for doing this?

10条回答
你好瞎i
2楼-- · 2020-05-25 04:05

You can use 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)
查看更多
混吃等死
3楼-- · 2020-05-25 04:05
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)
查看更多
萌系小妹纸
4楼-- · 2020-05-25 04:06

My solution to find the first and last day of the current month:

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
查看更多
做个烂人
5楼-- · 2020-05-25 04:08

The arrow module will steer you around and away from subtle mistakes, and it's easier to use that older products.

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

In this case there is no need for you to consider the varying lengths of months, for instance. Here's the output from this script.

25-Feb-2017 01-Feb-2017
28-Feb-2017 01-Mar-2017
查看更多
爱情/是我丢掉的垃圾
6楼-- · 2020-05-25 04:16

This is a pithy solution.

import datetime 

todayDate = datetime.date.today()
if todayDate.day > 25:
    todayDate += datetime.timedelta(7)
print todayDate.replace(day=1)

One thing to note with the original code example is that using timedelta(30) will cause trouble if you are testing the last day of January. That is why I am using a 7-day delta.

查看更多
地球回转人心会变
7楼-- · 2020-05-25 04:24

This could be an alternative to Gustavo Eduardo Belduma's answer:

import datetime 
first_day_of_the_month = datetime.date.today().replace(day=1)
查看更多
登录 后发表回答