Python implementation of date determination and addition and subtraction operations
#==================================================== #Time related #==================================================== def if_workday(day_str, separator=""): """ if a day is workday :param day_str: string of a day :param separator: separator of year, month and day, default is empty :return: True: is workday; False: not workday """ spec = "%Y" + separator + "%m" + separator + "%d" day = (day_str, spec).date() # Monday == 0 ... Sunday == 6 if () in [0, 1, 2, 3, 4]: return True else: return False def if_weekend(day_str, separator=""): """ if a day is weekend :param day_str: string of a day :param separator: separator of year, month and day, default is empty :return: True: is weekend; False: not weekend """ spec = "%Y" + separator + "%m" + separator + "%d" day = (day_str, spec).date() # Monday == 0 ... Sunday == 6 if () in [5, 6]: return True else: return False def is_week_lastday(): ''' Determine if today is the weekend,return the day of the week as an integer,Monday is 0 and Sunday is 6 :return: ''' now = (() + (hours=8)) # If it's Sunday todayIndex = () # If today is Sunday, return True if todayIndex == 6: print("todayIndex={},It's the weekend....".format(todayIndex)) return True else: print("todayIndex={},It's the week. {},It's not the weekend....".format(todayIndex,int(todayIndex+1))) return False def is_week_whichday(dayIndex=6): ''' Determine which day of the week it is today, Monday is 0 and the weekend is 6, return the day of the week as an integer,Monday is 0 and Sunday is 6 :return. ''' now = (() + (hours=8)) # If it's Sunday todayIndex = () # If today is Sunday, return True if todayIndex == dayIndex: print("todayIndex={},It's the week. {},It's not Chow. {}...".format(todayIndex, int(todayIndex+1),int(dayIndex+1))) return True else: print("todayIndex={},It's the week. {},It's not Chow. {}...".format(todayIndex,int(todayIndex+1),int(dayIndex+1))) return False def is_month_lastday(): ''' # Determine if today is the end of the month :return: ''' # Get the date of the 1st of the month start_date = ().replace(day=1) # Get the total number of days in the month (i.e., the date of the last day) _, days_in_month = (start_date.year, start_date.month) todayIndex = ("%d", ()) # If it's the weekend, return True if int(todayIndex) == int(days_in_month): print("start_date={},todayIndex={},days_in_month={},It's the end of the month....".format(start_date,todayIndex, days_in_month)) return True else: print("start_date={},todayIndex={},days_in_month={},It's not the end of the month....".format(start_date,todayIndex , days_in_month)) return False def get_this_week_start(): ''' Get the date of the first day of the week :return. ''' now = () this_week_start = now - timedelta(days=()) this_week_end = now + timedelta(days=6 - ()) print('--- this_week_start = {} this_week_end = {}'.format(this_week_start, this_week_end)) print('--- this_week_start = {} '.format(this_week_start)) return this_week_start def get_this_week_end(): ''' # Get the last day of the week :return: ''' now = () this_week_start = now - timedelta(days=()) this_week_end = now + timedelta(days=6 - ()) print('--- this_week_start = {} this_week_end = {}'.format(this_week_start, this_week_end)) print('--- this_week_end = {}'.format(this_week_end)) return this_week_end def get_last_month_start(now = ()): now = (now, "%Y-%m-%d") # First and last day of the month this_month_start = (, , 1) this_month_end = (, + 1, 1) - timedelta(days=1) + (hours=23, minutes=59, seconds=59) # print('--- this_month_start = {} this_month_end = {}'.format(this_month_start, this_month_end)) # First and last day of the previous month last_month_end = this_month_start - timedelta(days=1)+ (hours=23, minutes=59, seconds=59) last_month_start = (last_month_end.year, last_month_end.month, 1) # print('--- last_month_end = {} last_month_start = {}'.format(last_month_end, last_month_start)) print('--- last_month_start = {}'.format(last_month_start)) return last_month_start def get_last_month_end(now = ()): now = (now, "%Y-%m-%d") # First and last day of the month this_month_start = (, , 1) this_month_end = (, + 1, 1) - timedelta(days=1) + (hours=23, minutes=59, seconds=59) # print('--- this_month_start = {} this_month_end = {}'.format(this_month_start, this_month_end)) # First and last day of the previous month last_month_end = this_month_start - timedelta(days=1) + (hours=23, minutes=59, seconds=59) last_month_start = (last_month_end.year, last_month_end.month, 1) # print('--- last_month_end = {} last_month_start = {}'.format(last_month_end, last_month_start)) print('--- last_month_end = {} '.format(last_month_end)) return last_month_end def get_this_month_start(now = ()): now = (now, "%Y-%m-%d") # First and last day of the month this_month_start = (, , 1) this_month_end = (, + 1, 1) - timedelta(days=1) + (hours=23, minutes=59, seconds=59) # print('--- this_month_start = {} this_month_end = {}'.format(this_month_start, this_month_end)) # First and last day of the previous month last_month_end = this_month_start - timedelta(days=1) + (hours=23, minutes=59, seconds=59) last_month_start = (last_month_end.year, last_month_end.month, 1) # print('--- last_month_end = {} last_month_start = {}'.format(last_month_end, last_month_start)) print('--- this_month_start = {} '.format(this_month_start)) return this_month_start def get_this_month_end(now = ()): now=(now, "%Y-%m-%d") # First and last day of the month this_month_start = (, , 1) # this_month_end = (, + 1, 1) - timedelta(days=1) + (hours=23, minutes=59, seconds=59) if < 12: this_month_end = (, + 1, 1) - timedelta(days=1) + (hours=23, minutes=59, seconds=59) elif >= 12: this_month_end = (, , +30) + (hours=23, minutes=59, seconds=59) # print('--- this_month_start = {} this_month_end = {}'.format(this_month_start, this_month_end)) # First and last day of the previous month last_month_end = this_month_start - timedelta(days=1) + (hours=23, minutes=59, seconds=59) last_month_start = (last_month_end.year, last_month_end.month, 1) # print('--- last_month_end = {} last_month_start = {}'.format(last_month_end, last_month_start)) # print('--- this_month_end = {} '.format(this_month_end)) return str(this_month_end) # Get each day from a time period, with customizable intervals. def get_every_day(start = '2018-01-01',end = '2021-01-01',daysCount=1): ''' Get each day from a time period, with customizable intervals. :param start: str type, start time, e.g. '2018-01-01' :param end: str type, end time, e.g. '2021-01-01' :param daysCount: int type, every time interval, default is 1 day :return. ''' datestart = (start, '%Y-%m-%d') dateend = (end, '%Y-%m-%d') date_list=[] while datestart < dateend: datestart += (days=daysCount) date_str=str(('%Y-%m-%d')) # print('date_str={}'.format(date_str)) date_list.append(date_str) print('date_list={}'.format(date_list)) return date_list # Get each month from a time period, with customizable time intervals. def getBetweenEveryMonth(begin_date,end_date): date_list = [] begin_date = (begin_date, "%Y-%m-%d") end_date = (end_date, "%Y-%m-%d") # end_date = (('%Y-%m-%d', (())), "%Y-%m-%d") while begin_date <= end_date: date_str = begin_date.strftime("%Y-%m-%d") begin_date = add_months_start(begin_date, 1) date_end=get_this_month_end(date_str) date_list.append((date_str+' 00:00:00',date_end)) print('date_list={}'.format(date_list)) return date_list def add_months_start(dt, months): month = int( - 1 + months) year = int( + month / 12) month = int(month % 12 + 1) day = min(, (year, month)[1]) return (year=year, month=month, day=day) def add_months_end(dt, months): month = int( - 1 + months) year = int( + month / 12) month = int(month % 12 + 1) day = max(, (year, month)[1]) return (year=year, month=month, day=day)
This article on the Python date judgment and add or subtract operation details of the article is introduced to this, more related Python date judgment add or subtract operation content, please search for my previous posts or continue to browse the following related articles I hope that you will support me in the future more!