1.datetime模块主要是5个类
date #日期类 年月日 datetime.date(year,month,day)
time #时间类 时分秒 datetime.time(hour,minute,second,microsecond,tzoninfo),返回18:29:30
datetime #日期时间类 datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])
timedelta #两个时间点的间隔
tzinfo #时区
2.使用到的方法
datetime.date.isocalendar() #返回年月日的元祖(2018,9,29)
datetime.date.isoweekday() #返回给定日期的星期(1~7)
datetime.date.strftime(format) #已指定日期返回
......使用到再记录
3.使用到的日期相关
%y 两位数的年份表示(00-99)
%Y 四位数的年份表示(000-9999)
%m 月份(01-12)
%d 月内中的一天(0-31)
%H 24小时制小时数(0-23)
%I 12小时制小时数(01-12)
%M 分钟数(00=59)
%S 秒(00-59)
4.使用中的例子
import datetime
class timeUtil:
#昨天
def day_get(self,d):
oneday = datetime.timedelta(days=1)
day = d - oneday
date_from = datetime.date(day.year, day.month, day.day).strftime('%Y%m%d')
date_to = datetime.date(day.year, day.month, day.day).strftime('%Y%m%d')
return '&endTime='.join([str(date_from), str(date_to)])
#本周
def week_get(self,d):
dayscount = datetime.timedelta(days=d.isoweekday() - 1)
dayto = d - dayscount
sixdays = datetime.timedelta(days=6)
dayfrom = dayto + sixdays
date_to = datetime.date(dayfrom.year, dayfrom.month, dayfrom.day).strftime('%Y%m%d')
date_from = datetime.date(dayto.year, dayto.month, dayto.day).strftime('%Y%m%d')
return '&endTime='.join([str(date_from), str(date_to)])
#当前月
def month_get(self,d):
dayscount = datetime.timedelta(days=d.day - 1)
dayfrom = d - dayscount
date_from = datetime.date(dayfrom.year, dayfrom.month, 1).strftime('%Y%m%d')
date_to = datetime.date(d.year, d.month, d.day).strftime('%Y%m%d')
print '&endTime='.join([str(date_from), str(date_to)])