finding first day of the month in python

后端 未结 11 870
挽巷
挽巷 2021-02-02 05:39

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 da

相关标签:
11条回答
  • 2021-02-02 05:44

    First day of next month:

    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
    

    Then we replace the month and the day

        start_date = models.DateField(default=datetime.today().replace(month=new_start_month, day=1, hour=0, minute=0, second=0, microsecond=0))
    
    0 讨论(0)
  • 2021-02-02 05:46

    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
    
    0 讨论(0)
  • 2021-02-02 05:47

    Use 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)
    
    0 讨论(0)
  • 2021-02-02 05:49
    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)
    
    0 讨论(0)
  • 2021-02-02 05:52

    Yes, first set a datetime to the start of the current month.

    Second test if current date day > 25 and get a true/false on that. If True then add add one month to the start of month datetime object. If false then use the datetime object with the value set to the beginning of the month.

    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
    
    0 讨论(0)
  • 2021-02-02 05:52

    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
    
    0 讨论(0)
提交回复
热议问题