谈谈python time datetime timestamp之间转换

python中的时间表示形式


  1. string
    字符串形式表示时间:time.ctime()
  2. datetime
    In [15]: datetime.datetime.now()
    Out[15]: datetime.datetime(2018, 11, 14, 16, 38, 16, 152438)
  3. time
    In [14]: time.localtime()
    Out[14]: time.struct_time(tm_year=2018, tm_mon=11, tm_mday=14, tm_hour=16, tm_min=36, tm_sec=18, tm_wday=2, tm_yday=318, tm_isdst=0)
  4. timestamp:
    自1970年1月1日(00:00:00 GMT)以来的秒数
    In [16]: time.time()
    Out[16]: 1542184899.844029

上述几种表现形式之间的转换


  1. string ==================> datetime
    datetime.strpftime(format)

    datetime.strptime(str, format)
       In [17]: t_str="2018-11-14 16:50"
       
       In [18]: datetime.datetime.strptime(t_str, "%Y-%m-%d %H:%M")
       Out[18]: datetime.datetime(2018, 11, 14, 16, 50)
    
  2. string ==================> time

    time.strptime(str, format)
        In [20]: time.strptime(t_str, "%Y-%m-%d %H:%M")
        Out[20]: time.struct_time(tm_year=2018, tm_mon=11, tm_mday=14, tm_hour=16, tm_min=50, tm_sec=0, tm_wday=2, tm_yday=318, tm_isdst=-1)
        ```
    
  3. string ===================> timestamp
    一般不能直接转换,都是通过先转换为datetime或者time后在进一步做转换

  4. datetime =================> string
    dt_obj.strftime(format)

        In [21]: t_datetime = datetime.datetime.now()
        
        In [25]: t_datetime.strftime("%Y-%m-%D %H:%M")
        Out[25]: '2018-11-11/14/18 17:00'
        In [26]: type(t_datetime.strftime("%Y-%m-%D %H:%M"))
        Out[26]: str
    
  5. datetime =================> time
    dt_obj.timetuple()

       In [27]: t_datetime.timetuple()
       Out[27]: time.struct_time(tm_year=2018, tm_mon=11, tm_mday=14, tm_hour=17, tm_min=0, tm_sec=11, tm_wday=2, tm_yday=318, tm_isdst=-1)
    
  6. datetime ================> timestamp
    python3中 新加了一个函数dt_obj.timestamp(),可以直接转换
    python2中 datetime ----timetuple()----->time_obj --------time.mktime(time_obj) ----->timestamp

  7. time ===================> string
    time.strftime(fromat, time_obj)

        In [39]: time.strftime("%Y-%m-%d %H:%M", t_obj)
        Out[39]: '2018-11-14 17:21'
    
  8. time ===================> datetime
    datetime.datetime(*t_obj[0:6])

        In [42]: datetime.datetime(*t_obj[0:6])
        Out[42]: datetime.datetime(2018, 11, 14, 17, 21, 39)
    
  9. time ====================> timestamp
    time.mktime(t_obj)

    In [43]: time.mktime(t_obj)
    Out[43]: 1542187299.0
  1. timestamp ================> datetime
    datetime.datetime.fromtimestamp(ts)
    In [44]: ts = time.mktime(t_obj)
    In [46]: datetime.datetime.fromtimestamp(ts)
    Out[46]: datetime.datetime(2018, 11, 14, 17, 21, 39)
  1. timestamp ================> time
    time.localtime(ts)
    time.gmtime(ts)
        In [47]: time.localtime(ts)
        Out[47]: time.struct_time(tm_year=2018, tm_mon=11, tm_mday=14, tm_hour=17, tm_min=21, tm_sec=39, tm_wday=2, tm_yday=318, tm_isdst=0)
        
        In [48]: time.gmtime(ts)
        Out[48]: time.struct_time(tm_year=2018, tm_mon=11, tm_mday=14, tm_hour=9, tm_min=21, tm_sec=39, tm_wday=2, tm_yday=318, tm_isdst=0)
    
  2. timestamp =================> string
    timestamp ------fromtimestamp---------------> datetime ----------------strftime---------------->string
    timestamp---------localtime-------------------> time-----------------------strftime------------------>string
    timestam-----------gmtime -------------------> time ----------------------strftime------------------>string

参考:
http://blog.sina.com.cn/s/blog_b09d460201018o0v.html
https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431937554888869fb52b812243dda6103214cd61d0c2000

你可能感兴趣的:(python学习)