开发者社区> 问答> 正文

Python 将时间戳转换为指定格式日期

Python 将时间戳转换为指定格式日期

展开
收起
游客ejnn55cgkof5g 2020-02-14 19:04:14 2762 0
1 条回答
写回答
取消 提交回答
  • import time
     
    # 获得当前时间时间戳
    now = int(time.time())
    #转换为其他日期格式,如:"%Y-%m-%d %H:%M:%S"
    timeArray = time.localtime(now)
    otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
    print(otherStyleTime)
    执行以上代码输出结果为:
    
    2019-05-21 18:02:49
    实例 2
    import datetime
     
    # 获得当前时间
    now = datetime.datetime.now()
    # 转换为指定的格式
    otherStyleTime = now.strftime("%Y-%m-%d %H:%M:%S")
    print(otherStyleTime)
    执行以上代码输出结果为:
    
    2019-05-21 18:03:48
    指定时间戳
    实例 3
    import time
     
    timeStamp = 1557502800
    timeArray = time.localtime(timeStamp)
    otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
    print(otherStyleTime)
    执行以上代码输出结果为:
    
    2019-05-10 23:40:00
    实例 4
    import datetime
     
    timeStamp = 1557502800
    dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
    otherStyleTime = dateArray.strftime("%Y-%m-%d %H:%M:%S")
    print(otherStyleTime)
    执行以上代码输出结果为:
    
    2019-05-10 23:40:00
    
    2020-02-14 19:05:32
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载