美文网首页
python将时间戳转换为指定格式的类型

python将时间戳转换为指定格式的类型

作者: 逆向与爬虫的故事 | 来源:发表于2019-03-14 16:39 被阅读0次

    大家在用python写程序难免会遇到时间戳这样的数据,那么咋样转换为指定的格式呢,请看下面!

    使用time

    timeStamp = 1381419600
    timeArray = time.localtime(timeStamp)
    otherStyleTime = time.strftime("%Y--%m--%d %H:%M:%S", timeArray)
    print otherStyleTime # 2013--10--10 23:40:00

    使用datetime

    timeStamp = 1381419600
    dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
    otherStyleTime = dateArray.strftime("%Y--%m--%d %H:%M:%S")
    print otherStyleTime # 2013--10--10 15:40:00

    相关文章

      网友评论

          本文标题:python将时间戳转换为指定格式的类型

          本文链接:https://www.haomeiwen.com/subject/nbdcmqtx.html