zoukankan      html  css  js  c++  java
  • Python获取秒级时间戳与毫秒级时间戳

    1、获取秒级时间戳与毫秒级时间戳

    复制代码
    import time
    import datetime
    
    t = time.time()
    
    print (t)                       #原始时间数据
    print (int(t))                  #秒级时间戳
    print (int(round(t * 1000)))    #毫秒级时间戳
    
    nowTime = lambda:int(round(t * 1000))
    print (nowTime());              #毫秒级时间戳,基于lambda
    
    print (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))   #日期格式化
    复制代码

    返回

    1499825149.26
    1499825149
    1499825149257
    1499825149257
    2017-07-12 10:05:49

    2、将日期转为秒级时间戳

    dt = '2018-01-01 10:40:30'
    ts = int(time.mktime(time.strptime(dt, "%Y-%m-%d %H:%M:%S")))
    print (ts)

    返回

    1514774430

    3、将秒级时间戳转为日期

    ts = 1515774430
    dt = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts))
    print(dt)

    返回

    2018-01-13 00:27:10
  • 相关阅读:
    【Lua】LuaForWindows_v5.1.4-46安装失败解决方案
    【C++】指针引发的bug
    【C++】指针引发的bug
    【C++】位操作(3)-获取某位的值
    bzoj1444
    bzoj1758
    bzoj3091
    poj1741 bzoj2152
    bzoj2125 3047
    bzoj3669
  • 原文地址:https://www.cnblogs.com/sunyllove/p/9770787.html
Copyright © 2011-2022 走看看