python时间操作

发布时间:2019-09-18 07:27:56编辑:auto阅读(1543)

     

    1. #!/usr/bin/python 
    2. # -*- coding:utf8 -*- 
    3. import time,datetime 
    4. Atime=time.time() ##获取本地时间戳 
    5. print Atime 
    6. print time.strftime('%Y-%m-%d %H:%M:%S',time.gmtime(Atime)) ##将时间戳转换成2013-01-07 08:21:31 
    7. Nowtime=time.strftime('%Y-%m-%d %H:%M:%S',time.gmtime(Atime))  
    8. print time.mktime(time.strptime(Nowtime,"%Y-%m-%d %H:%M:%S")) ##将2013-01-07 08:21:31格式转换成时间戳 
    9.  
    10. print "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" 
    11. print time.ctime() #格式如:Mon Jan 07 15:20:16 2013 
    12. 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) 
    13. print time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()) #转换成:2013-01-07 15:20:16 
    14. print time.strftime("%a %b %d %H:%M:%S %Y",time.localtime())#转换成:Mon Jan 07 15:20:16 2013 
    15. a=time.ctime() 
    16. b=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()) 
    17. print time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y")) #将Mon Jan 07 15:20:16 2013这种格式转换成时间戳 
    18. print time.mktime(time.strptime(b,"%Y-%m-%d %H:%M:%S")) #将2013-01-07 15:20:16这种格式转换成时间戳 
    19.  
    20. print "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" 
    21. print datetime.datetime.now() #获取现在时间精确到微秒 
    22. Atime=datetime.datetime.now() 
    23. Btime=datetime.datetime.now().microsecond/1000    #microsecond是微秒需要除以1000才能取出毫秒数 
    24. Ctime=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()) 
    25. 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

关键字