time模块:
import time ces = time.time() #时间戳 是秒 从 1970.1.1 0.0.0 一直到现在的一个秒 # print(ces) # time.sleep(1) # print('haha...') ces1 = time.ctime() print(ces1) # Tue Dec 19 15:31:49 2017 ces2 = time.localtime() print(ces2) # time.struct_time(tm_year=2017, tm_mon=12, tm_mday=19, tm_hour=15, # tm_min=32, tm_sec=59, tm_wday=1, tm_yday=353, tm_isdst=0) print(ces2.tm_year) # 2017 print(time.strftime('%Y-%m-%d %H:%M:%S',ces2)) # 将ces2格式化为 2017-12-19 15:36:21 print(time.gmtime()) #等于 localtime print(time.strptime('2011-11-11 11:11:11', '%Y-%m-%d %H:%M:%S')) #功能与strftime相反
datetime:
import datetime ces = datetime.datetime.now() #获取当前系统时间: print(ces) #2017-12-19 16:09:27.312303 ces_geshi = ces.strftime('%Y-%m-%d %H:%M:%S') #将系统时间格式为: print(ces_geshi) #2017-12-19 16:09:27 ces_tuple=ces.timetuple() #将系统时间转为元祖: print(ces_tuple) #time.struct_time(tm_year=2017, tm_mon=12, tm_mday=19, tm_hour=16, tm_min=9, tm_sec=27, # tm_wday=1, tm_yday=353, tm_isdst=-1) ces2 = time.mktime(ces_tuple) #将元祖转为 时间戳: print(ces2) #1513670967.0 ces3 = datetime.datetime.fromtimestamp(ces2) print(ces3) # 将时间戳转为 2017-12-19 16:09:27 print(datetime.datetime.fromtimestamp(time.time())) #time.time()时间戳 2017-12-19 16:09:27.312304 ces1 = datetime.datetime(2011, 11, 11, 11, 11, 11) # 格式化 print(ces1) # 2011-11-11 11:11:11 print(datetime.date.today()) #今天的日期 2017-12-19 print(datetime.date.today()+datetime.timedelta(days=2)) #印后天日期2017-12-21 print(datetime.date.today()-datetime.timedelta(days=2)) #印前天日期2017-12-17