开发者社区 问答 正文

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

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

展开
收起
游客ejnn55cgkof5g 2020-02-14 19:04:14 2796 分享
分享
版权
举报
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 举报
    赞同 评论

    评论

    全部评论 (0)

    登录后可评论
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等