python中datetime模块的使用方法
导入包 import datetime 获取当天日期 now_time=datetime.datetime.now() print((now_time+datetime.timedelta(days=+1)).strftime("%Y-%m-%d %H:%M:%S")) #获取后一天+1 前一天-1 print ((now_time+datetime.timedelta(hours=-1)).strftime("%Y-%m-%d %H:%M:%S")) #获取前一小时 print(now_time.strftime("%Y-%m-%d %H:%M:%S"),(now_time+datetime.timedelta(minutes=+1)).strftime("%Y-%m-%d %H:%M:%S")) #前一分钟后一分钟 print(now_time.strftime("%Y-%m-%d %H:%M:%S"),(now_time+datetime.timedelta(minutes=+1/60)).strftime("%Y-%m-%d %H:%M:%S"))#前一秒后一秒
# 导入包 import calendar import datetime #获取一个月或是N个月,一年中的天数 years = [2019,] months = range(1, 13) for year in years: for month in months: days = calendar.monthrange(int(year), int(month))[1] for day in range(1,(days)+1): format_datetime = datetime.datetime(year, month, day).strftime('%Y-%m-%d') print(format_datetime)
import calendar import datetime years = [2019,] months = range(1, 13) for year in settings.years: for month in settings.months: day = calendar.monthrange(int(year), int(month))[1] fromdate = datetime.date(year, month, 1) # 开始时间: 2019-01-01 todate = fromdate + datetime.timedelta(days=day + 1) # 结束时间: 2019-02-02