Python内置模块-time && datatime

Python提供两种时间表示方式,一种是时间戳,从1970年1月1日 0时开始。一种是struct_time数组格式,共有9个元素。
import time
print(time.time()) #返回时间戳,从1970年1月1日00:00:00开始。返回值为float。
print(time.ctime()) #输出当前系统时间。
print(time.ctime(time.time()-86400))#当前时间减去一天,返回字符串格式。
print(time.gmtime()) #返回数组形式的struct_time,共九个元素。为UTC时间
# tm_year, tm_mon, tm_mday, tm_hour, tm_min,tm_sec, tm_wday, tm_yday, tm_isdst
now = time.gmtime()
print(now.tm_year)
print(time.localtime()) #返回数组形式的struct_time,共九个元素。为本地时间
print(time.mktime(time.localtime()))  #将struct_time转换成字符串格式的时间戳
print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()))  #将Struct_time格式转换为指定格式输出。UTC时间。
print(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()))  #将Struct_time格式转换为指定格式输出。本地时间

time.sleep(4) #程序休眠4s

import datetime
print(datetime.date.today()) #返回格式:2016-06-07
print(datetime.date.fromtimestamp(time.time()))
print(datetime.datetime.now()) #返回当前时间2016-06-07 15:34:24.228779
print(datetime.datetime.now().timetuple()) #返回struct_time格式的时间
new_date = datetime.datetime.now() + datetime.timedelta(days=10) #比现在加10天
new_date = datetime.datetime.now() + datetime.timedelta(days=-10) #比现在减10天
new_date = datetime.datetime.now() + datetime.timedelta(hours=-10) #比现在减10小时
new_date = datetime.datetime.now() + datetime.timedelta(seconds=120) #比现在+120s

print(new_date)