加载包
from datetime import date, timedelta
一 、基础函数
current_date = date(2015, 8, 22)
date1 = current_date
date2 = date(2015, 8, 10)
dd = current_date
def first_day_of_current_week(current_date):
return current_date - timedelta(days=current_date.weekday())
def first_day_of_current_month(current_date):
return current_date.replace(day=1)
def days_between_two_dates(date1, date2):
n_days = abs((date1 - date2).days)
end = date2 if date2 > date1 else date1
if n_days == 1:
return str(end - timedelta(days=1))
return [str(end - timedelta(days=x)) for x in range(n_days, 0, -1)]
>>> first_day_of_current_week(current_date)
datetime.date(2015, 8, 17)
>>> first_day_of_current_month(current_date)
datetime.date(2015, 8, 1)
>>> days_between_two_dates(date1, date2)
['2015-08-10', '2015-08-11', '2015-08-12', '2015-08-13', '2015-08-14', '2015-08-15', '2015-08-16', '2015-08-17', '2015-08-18', '2015-08-19', '2015-08-20', '2015-08-21']
二、 核心函数
def days_of_this_week(current_date, just2dd=True):
first_day_of_this_week = first_day_of_current_week(current_date)
first_day_of_next_week = first_day_of_this_week + timedelta(days=7)
if just2dd:
return days_between_two_dates(first_day_of_this_week, current_date)
return days_between_two_dates(first_day_of_this_week,
first_day_of_next_week)
def days_of_last_week(current_date):
first_day_of_this_week = first_day_of_current_week(current_date)
first_day_of_next_week = first_day_of_this_week - timedelta(days=7)
return days_of_this_week(first_day_of_next_week, just2dd=False)
>>> days_of_this_week(current_date, just2dd=True)
['2015-08-17', '2015-08-18', '2015-08-19', '2015-08-20', '2015-08-21']
>>> days_of_last_week(current_date)
['2015-08-10', '2015-08-11', '2015-08-12', '2015-08-13', '2015-08-14', '2015-08-15', '2015-08-16']
def days_of_this_month(dd=date.today(), just2dd=True):
first_day_of_this_month = first_day_of_current_month(dd)
if dd.month == 12:
first_day_of_next_month = dd.replace(year=dd.year+1, month=1, day=1)
else:
first_day_of_next_month = dd.replace(month=dd.month+1, day=1)
if just2dd:
return days_between_two_dates(dd, first_day_of_this_month)
return days_between_two_dates(first_day_of_this_month,
first_day_of_next_month)
def days_of_last_month(dd=date.today()):
last_day_of_last_month = first_day_of_current_month(dd) - timedelta(days=1)
return days_of_this_month(last_day_of_last_month, just2dd=False)
>>> days_of_this_month(dd=date.today(), just2dd=True)
['2015-08-01', '2015-08-02', '2015-08-03', '2015-08-04', '2015-08-05', '2015-08-06', '2015-08-07', '2015-08-08', '2015-08-09', '2015-08-10', '2015-08-11', '2015-08-12', '2015-08-13', '2015-08-14', '2015-08-15', '2015-08-16', '2015-08-17', '2015-08-18', '2015-08-19', '2015-08-20', '2015-08-21']
>>> days_of_last_month(dd=date.today())
['2015-07-01', '2015-07-02', '2015-07-03', '2015-07-04', '2015-07-05', '2015-07-06', '2015-07-07', '2015-07-08', '2015-07-09', '2015-07-10', '2015-07-11', '2015-07-12', '2015-07-13', '2015-07-14', '2015-07-15', '2015-07-16', '2015-07-17', '2015-07-18', '2015-07-19', '2015-07-20', '2015-07-21', '2015-07-22', '2015-07-23', '2015-07-24', '2015-07-25', '2015-07-26', '2015-07-27', '2015-07-28', '2015-07-29', '2015-07-30', '2015-07-31']
- 获取当前日期前n天的日期字符串列表(左闭右开,即不包含当前日期)
def last_n_days(current_date, n=0):
if n in (0,1):
return str(current_date - timedelta(days=n))
return [str(current_date - timedelta(x)) for x in range(n, 0, -1)]
>>> last_n_days(current_date, n=1)
'2015-08-21'
>>> last_n_days(current_date, n=4)
['2015-08-18', '2015-08-19', '2015-08-20', '2015-08-21']