finding first day of the month in python

后端 未结 11 1028
挽巷
挽巷 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: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
    

提交回复
热议问题