import time
时间相关的操作,时间有三种表示方式:
- 时间戳 1970年1月1日之后的秒,即:time.time()
- 格式化的字符串 2014-11-11 11:11, 即:time.strftime('%Y-%m-%d')
- 结构化时间 元组包含了:年、日、星期等... time.struct_time 即:time.localtime()
1 print time.time() 2 print time.mktime(time.localtime()) 3 4 print time.gmtime() #可加时间戳参数 5 print time.localtime() #可加时间戳参数 6 print time.strptime('2014-11-11', '%Y-%m-%d') 7 8 print time.strftime('%Y-%m-%d') #默认当前时间 9 print time.strftime('%Y-%m-%d',time.localtime()) #默认当前时间 10 print time.asctime() 11 print time.asctime(time.localtime()) 12 print time.ctime(time.time()) 13 14 import datetime 15 ''' 16 datetime.date:表示日期的类。常用的属性有year, month, day 17 datetime.time:表示时间的类。常用的属性有hour, minute, second, microsecond 18 datetime.datetime:表示日期时间 19 datetime.timedelta:表示时间间隔,即两个时间点之间的长度 20 timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]]) 21 strftime("%Y-%m-%d") 22 ''' 23 import datetime 24 print datetime.datetime.now() 25 print datetime.datetime.now() - datetime.timedelta(days=5)
%Y Year with century as a decimal number. %m Month as a decimal number [01,12]. %d Day of the month as a decimal number [01,31]. %H Hour (24-hour clock) as a decimal number [00,23]. %M Minute as a decimal number [00,59]. %S Second as a decimal number [00,61]. %z Time zone offset from UTC. %a Locale's abbreviated weekday name. %A Locale's full weekday name. %b Locale's abbreviated month name. %B Locale's full month name. %c Locale's appropriate date and time representation. %I Hour (12-hour clock) as a decimal number [01,12]. %p Locale's equivalent of either AM or PM.