pythonでよく使われる関数

1385 ワード

python timestampとdatetime間の変換
  • 文字列日付時刻をタイムスタンプ
  • に変換
    # '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 ''  

    転載先:https://www.cnblogs.com/daihanlong/p/8564163.html