首页 > 编程语言 > 详细

python 常用的函数

时间:2018-03-14 00:38:20      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:exc   --   code   etime   str   times   pre   字符串日期   pan   

python timestamp和datetime之间的转换

  1. 字符串日期时间转换成时间戳

    # '2015-08-28 16:43:37.283' --> 1440751417.283  
    # 或者 '2015-08-28 16:43:37' --> 1440751417.0  
    def string2timestamp(strValue):  
    
    try:          
        d = datetime.datetime.strptime(strValue, "%Y-%m-%d %H:%M:%S.%f")  
        t = d.timetuple()  
        timeStamp = int(time.mktime(t))  
        timeStamp = float(str(timeStamp) + str("%06d" % d.microsecond))/1000000  
        print timeStamp  
        return timeStamp  
    except ValueError as e:  
        print e  
        d = datetime.datetime.strptime(str2, "%Y-%m-%d %H:%M:%S")  
        t = d.timetuple()  
        timeStamp = int(time.mktime(t))  
        timeStamp = float(str(timeStamp) + str("%06d" % d.microsecond))/1000000  
        print timeStamp  
        return timeStamp  

2.时间戳转换成字符串日期时间

# 1440751417.283 --> '2015-08-28 16:43:37.283'  
def timestamp2string(timeStamp):  
    try:  
        d = datetime.datetime.fromtimestamp(timeStamp)  
        str1 = d.strftime("%Y-%m-%d %H:%M:%S.%f")  
        # 2015-08-28 16:43:37.283000'  
        return str1  
    except Exception as e:  
        print e  
        return ''  

python 常用的函数

标签:exc   --   code   etime   str   times   pre   字符串日期   pan   

原文:https://www.cnblogs.com/daihanlong/p/8564163.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号