目的:使用Python计算本月及上月1号这两个时间点统计数据。因此应把day限制住。
1、设置当前月份1号
import datetime
from dateutil.relativedelta import relativedelta
now = datetime.datetime.now()
now_month = now.strftime('%Y-%m-01') # 计算本月1号
print(now_month)
2、计算上月1号,使用months = -1,day限制住为1。
last_month=datetime.datetime.strftime(now +relativedelta(months=-1), "%Y-%m-01")
print(last_month)
3、计算当前时间前15天的日期时间。采用days=-15
lw_15 = datetime.datetime.now() + datetime.timedelta(days=-15)
last_15 = lw_15.strftime('%Y-%m-%d')
print(last_15)