目录
  • 当前时间
    • 实例1:
    • 实例2:
  • 指定时间戳
    • 实例1:
    • 实例2:
  • 总结

    我们将会启用到time库:

    当前时间

    实例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

    指定时间戳

    实例1:

    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

    实例2:

    import datetime
    timeStamp = 1557502800
    dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
    otherStyleTime = dateArray.strftime("%Y-%m-%d %H:%M:%S")
    print(otherStyleTime)

    总结

    本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注的更多内容!

    声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。