时间模块

时间戳:通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量

格式化的时间字符串

元祖(struct_time)

 参考:http://blog.51cto.com/egon09/1840425

  • time
import time
a = time.time()#得出时间戳,时间戳是从1970年开始计算:1514617654.7603595
b = a /3600/24/366#47.89698614780534
c = 1970+b#2017.8969861478054
print(a)
print(b)
print(c)
time()

localtime:本地时间转换为元祖方式显示,可以加入时间戳为参数

import time
a = time.localtime()
print(a)#time.struct_time(tm_year=2017, tm_mon=12, tm_mday=30, tm_hour=15, tm_min=16, tm_sec=30, tm_wday=5, tm_yday=364, tm_isdst=0)
localtime()
print(a.tm_year,a.tm_mon,a.tm_mday)#可以这样读出元祖的数据
import time
a = time.timezone/3600  #与UTC时间差,中国与格林治时间相差8小时,所以这里是8
b = time.altzone/3600 #UTC与夏令时DST差,这里是9
c = time.daylight#因为没有使用夏令时DST,所以这里是0
print(a)
print(b)
print(c)


gmtime()#当前UTC标准时间转为元祖显示,可以加入时间戳为参数
import time

a = time.gmtime()#当前UTC标准时间转为元祖显示:time.struct_time(tm_year=2017, tm_mon=12, tm_mday=30, tm_hour=7, tm_min=25, tm_sec=3, tm_wday=5, tm_yday=364, tm_isdst=0)
print(a)
gmtime()
strftime(格式化,struct_time)
import time
a = time.localtime()
b = time.strftime("%Y-%m-%d %H:%M:%S  Time zone offset from UTC:%z",a)
print(a)#time.struct_time(tm_year=2017, tm_mon=12, tm_mday=30, tm_hour=15, tm_min=16, tm_sec=30, tm_wday=5, tm_yday=364, tm_isdst=0)
print(a.tm_year,a.tm_mon,a.tm_mday)
print(b)
#print(help(time.strftime))

 

asctime()#元祖-->字符串Sat Dec 30 15:39:46 2017
ctime()#时间戳-->字符串Sat Dec 30 15:39:46 2017
import time

a = time.asctime()#元祖-->字符串Sat Dec 30 15:39:46 2017
print(a)
b=time.ctime()#时间戳-->字符串Sat Dec 30 15:39:46 2017
print(b)

 

 

  • datetime

 

import datetime

a = datetime.datetime.now()#获取当前时间
a.replace(year=2020,month=1,day=20,hour=12,minute=30,second=20)
print(a.replace(year=2020,month=1,day=20,hour=12,minute=30,second=20))
aa = datetime.datetime.now().replace(year=int(datetime.datetime.now().strftime("%Y"))+10).strftime("%Y-%m-%d")
print(aa)
b = datetime.datetime.now() + datetime.timedelta(3)#获取3天后时间
print(b)
c = datetime.datetime.now() + datetime.timedelta(-3)#获取3天前时间
print(c)
d = datetime.datetime.now() + datetime.timedelta(hours=3)#获取3小时后时间
print(d)
e = datetime.datetime.now() + datetime.timedelta(hours=-3)#获取3小时前时间
print(e)
f = datetime.datetime.now() + datetime.timedelta(minutes=30)#获取30分钟后前时间
print(f)

 

posted @ 2017-12-30 15:46  雨之愿风  阅读(219)  评论(0编辑  收藏  举报