import time,datetime a=time.time()##时间戳 print(a) b = time.gmtime()###括号没有传参数就返回utc标准时间 print(b) c = time.localtime(545215562)###返回本地时间##获取年tm_year print(c) print(c.tm_year) d = time.mktime(b)###以一个元组的时间转换为时间戳 print(d) ##%Y_%m_%d_%H_%M_%S_%Z_%a_%A e = time.strftime("%Y_%m_%d %H:%M:%S:%Z",c)###转换成格式化字符串形式 print(e) f = time.strptime("1987_04_12 16:46:02","%Y_%m_%d %H:%M:%S")##同上相反 print(f)
###时间加减 print(datetime.datetime.now())##2018-04-26 10:31:59.594180 print(datetime.date.fromtimestamp(time.time()))###时间戳直接转为2018-04-26 print(datetime.datetime.now()+datetime.timedelta(3))##2018-04-29 10:41:55.938860###就是当前时间加上后面的天数3 print(datetime.datetime.now()+datetime.timedelta(-3))###同上相反 print(datetime.datetime.now()+datetime.timedelta(hours=3))##当前时间加上3小时 减就在3前面加上减号 print(datetime.datetime.now()+datetime.timedelta(minutes=30))##当前时间加上30分钟