python中計算上個月和下個月的第一天的方法
import datetime,time def get_1st_of_last_month(): """ 獲取上個月第一天的日期,然后加21天就是22號的日期 :return: 返回日期 """ today=datetime.datetime.today() year=today.year month=today.month if month==1: month=12 year-=1 else: month-=1 res=datetime.datetime(year,month,1)+datetime.timedelta(days=21) return res ############################# def get_1st_of_next_month(): """ 獲取下個月的22號的日期 :return: 返回日期 """ today=datetime.datetime.today() year=today.year month=today.month if month==12: month=1 year+=1 else: month+=1 res=datetime.datetime(year,month,1)+datetime.timedelta(days=21) return res ############################# def time_s_to_stamp(args): """ 將datetime日期格式,先timetuple()轉化為struct_time格式 然后time.mktime轉化為時間戳 :param args: datetime時間格式數據 :return: 時間戳格式數據 """ res=time.mktime(args.timetuple()) return res