zoukankan      html  css  js  c++  java
  • 时间戳与时间互转

    参考 https://blog.csdn.net/xxm524/article/details/48055275

    # 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
    
    
    
    
    
    
    #import pytz
    #from datetime import datetime
    #print(datetime.fromtimestamp(0, pytz.timezone('Asia/Shanghai')))
    
    # 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 ''
  • 相关阅读:
    S MVC 转发与重定向
    S MVC Controller方法返回值
    S MVC 表单提交
    numpy数据平滑
    Python入门
    Django
    python机器学习基础教程-监督学习
    drf-CBV
    numpy数组符号化与函数向量化
    numpy中多项式拟合与复数
  • 原文地址:https://www.cnblogs.com/lajiao/p/9554013.html
Copyright © 2011-2022 走看看