代码语言:javascript复制
#!/usr/bin/python # -*- coding:utf8 -*- import time,datetime Atime=time.time() ##获取本地时间戳 print Atime print time.strftime('%Y-%m-%d %H:%M:%S',time.gmtime(Atime)) ##将时间戳转换成2013-01-07 08:21:31 Nowtime=time.strftime('%Y-%m-%d %H:%M:%S',time.gmtime(Atime)) print time.mktime(time.strptime(Nowtime,"%Y-%m-%d %H:%M:%S")) ##将2013-01-07 08:21:31格式转换成时间戳 print " " print time.ctime() #格式如:Mon Jan 07 15:20:16 2013 print time.localtime() #格式如:time.struct_time(tm_year=2013, tm_mon=1, tm_mday=7, tm_hour=15, tm_min=20, tm_sec=16, tm_wday=0, tm_yday=7, tm_isdst=0) print time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()) #转换成:2013-01-07 15:20:16 print time.strftime("%a %b %d %H:%M:%S %Y",time.localtime())#转换成:Mon Jan 07 15:20:16 2013 a=time.ctime() b=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()) print time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y")) #将Mon Jan 07 15:20:16 2013这种格式转换成时间戳 print time.mktime(time.strptime(b,"%Y-%m-%d %H:%M:%S")) #将2013-01-07 15:20:16这种格式转换成时间戳 print " " print datetime.datetime.now() #获取现在时间精确到微秒 Atime=datetime.datetime.now() Btime=datetime.datetime.now().microsecond/1000 #microsecond是微秒需要除以1000才能取出毫秒数 Ctime=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()) print str(Ctime) str(Btime)
结果如下:
1357548276.53 2013-01-07 08:44:36 1357519476.0 Mon Jan 07 16:44:36 2013 time.struct_time(tm_year=2013, tm_mon=1, tm_mday=7, tm_hour=16, tm_min=44, tm_sec=36, tm_wday=0, tm_yday=7, tm_isdst=0) 2013-01-07 16:44:36 Mon Jan 07 16:44:36 2013 1357548276.0 1357548276.0 2013-01-07 16:44:36.562000 2013-01-07 16:44:36562