Python模块-datetime模块
datetime.datetime.now() 返回当前的datetime日期类型
>>> d=datetime.datetime.now() >>> d datetime.datetime(2018, 2, 17, 17, 41, 49, 372222) >>> d.year 2018 >>> d.month 2 >>> d.timetuple() time.struct_time(tm_year=2018, tm_mon=2, tm_mday=17, tm_hour=17, tm_min=41, tm_sec=49, tm_wday=5, tm_yday=48, tm_isdst=-1) >>> d.timestamp() #转换为时间戳 1518860509.372222 >>> d.replace(year=1997,month=9,day=21) #时间替换 datetime.datetime(1997, 9, 21, 17, 41, 49, 372222)
datetime.date.fromtimestamp() 把一个时间戳转为datetime日期类型
>>> datetime.date.fromtimestamp(1518860509.372222) datetime.date(2018, 2, 17)
datetime.timedelta:表示时间间隔,即两个时间点之间的长度
>>> datetime.datetime.now() datetime.datetime(2018, 2, 17, 17, 49, 32, 336825) >>> datetime.datetime.now() - datetime.timedelta(3) #如果没有指定,默认为3天 datetime.datetime(2018, 2, 14, 17, 49, 56, 980665) >>> datetime.datetime.now() - datetime.timedelta(days=3) #减去3天 datetime.datetime(2018, 2, 14, 17, 50, 9, 23454) >>> datetime.datetime.now() + datetime.timedelta(hours=3) #加3个小时 datetime.datetime(2018, 2, 17, 20, 50, 19, 688480