标签:
1. time
time.time()
time.localtime()
>>> time.gmtime(time.time()) time.struct_time(tm_year=2016, tm_mon=5, tm_mday=24, tm_hour=9, tm_min=15, tm_sec=12, tm_wday=1, tm_yday=145, tm_isdst=0) >>> time.mktime(time.localtime()) 1464081346.0
>>> time.strftime("%Y%m%d", time.localtime()) ‘20160524‘ >>> time.strptime(‘20160524‘, "%Y%m%d") time.struct_time(tm_year=2016, tm_mon=5, tm_mday=24, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=145, tm_isdst=-1)
2. datetime
>>> datetime.datetime.now() datetime.datetime(2016, 5, 24, 17, 35, 10, 553391)
>>> datetime.datetime.now().strftime("%Y%m%d_%H%M%S") ‘20160524_173543‘
可以对时间进行比较
>>> time1 = datetime.datetime.now() >>> time2 = datetime.datetime.now() >>> time2 -time1 datetime.timedelta(0, 5, 944002) >>> time2 > time1 True >>> datetime.datetime.now() - datetime.timedelta(days = 2) datetime.datetime(2016, 5, 22, 17, 38, 57, 994761)
3. time ---> datetime
>>> datetime.datetime.fromtimestamp(time.time())
datetime.datetime(2016, 5, 24, 17, 40, 44, 138647)
标签:
原文地址:http://www.cnblogs.com/xurui/p/python_time_and_datetime_module.html