datetime的使用
>>> import datetime
>>> #获取当天时间
>>> now = datetime.datetime.now()
>>> now
datetime.datetime(2016, 12, 21, 9, 26, 0, 627403)
>>> #获取now所属类型
>>> type(now)
<type 'datetime.datetime'>
>>> #datetime格式化
>>> datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
'2016-12-21 09:31:00'
>>> #获取当天date
...
>>> datetime.date.today()
datetime.date(2016, 12, 21)
>>> #获取昨天
...
>>> datetime.date.today() - datetime.timedelta(days=1)
datetime.date(2016, 12, 20)
>>> datetime.date.today() + datetime.timedelta(days=-1)
datetime.date(2016, 12, 20)
>>> #获取当天开始时间
...
>>> datetime.datetime.combine(datetime.date.today(), datetime.time.min)
datetime.datetime(2016, 12, 21, 0, 0)
>>> #获取当天结束时间
...
>>> datetime.datetime.combine(datetime.date.today(), datetime.time.max)
datetime.datetime(2016, 12, 21, 23, 59, 59, 999999)
>>> #获取本周最后一天
>>> lastWeekDay = datetime.date.today() + datetime.timedelta(6- datetime.date.today().weekday())
>>> lastWeekDay
datetime.date(2016, 12, 25)
>>> #获取本月最后一天
>>> import calendar
>>> today = datetime.date.today()
>>> _, last_day_num = calendar.monthrange(today.year, today.month)
>>> last_day = datetime.date(today.year, today.month, last_day_num)
>>> last_day
datetime.date(2016, 12, 31)
>>> #获取上个月的最后一天(可能跨年)
>>> import datetime
>>> today = datetime.date.today()
>>> first = datetime.date(day=1, month=today.month, year=today.year)
>>> lastMonth = first - datetime.timedelta(days=1)