1. time
- 第一种是时间戳的方式(相对于1970.1.1 00:00:00以秒计算的偏移量),时间戳是惟一的
time.time()
- 第二种以数组的形式表示即(struct_time),共有九个元素,分别表示,同一个时间戳的struct_time会因为时区不同而不同
time.localtime()
- gmtime()与mktime()可以将两种时间表示方法自由转换
>>> 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
- strftime 和strptime
>>> 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)
时间: 2024-10-14 22:29:15