標籤:
#當前一周 def week_get_now(self): import datetime d = datetime.datetime.now() dayscount = datetime.timedelta(days=d.isoweekday()) dayto = d sixdays = datetime.timedelta(days=6) dayfrom = dayto - sixdays date_from = datetime.datetime(dayfrom.year, dayfrom.month, dayfrom.day, 0, 0, 0) date_to = datetime.datetime(dayto.year, dayto.month, dayto.day, 23, 59, 59) return str(date_from), str(date_to) #上一周時間段 def week_get(self): import datetime d = datetime.datetime.now() dayscount = datetime.timedelta(days=d.isoweekday()) dayto = d - dayscount sixdays = datetime.timedelta(days=6) dayfrom = dayto - sixdays date_from = datetime.datetime(dayfrom.year, dayfrom.month, dayfrom.day, 0, 0, 0) date_to = datetime.datetime(dayto.year, dayto.month, dayto.day, 23, 59, 59) return str(date_from), str(date_to) #上個月時間段 def month_get(self): import datetime d = datetime.datetime.now() dayscount = datetime.timedelta(days=d.day) dayto = d - dayscount date_from = datetime.datetime(dayto.year, dayto.month, 1, 0, 0, 0) date_to = datetime.datetime(dayto.year, dayto.month, dayto.day, 23, 59, 59) return str(date_from), str(date_to)
python擷取各個時間段