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

作者:一个兴趣使然的程序猿罢了 时间:2023-11-05 13:48:41 

目录
  • 当前时间

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

    总结

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

    来源:https://blog.csdn.net/m0_64660514/article/details/121916717

    标签:Python,转换,时间戳
    0
    投稿

    猜你喜欢

  • 如何基于python对接钉钉并获取access_token

    2023-11-27 04:25:07
  • vue3动态添加路由

    2024-05-13 09:08:34
  • Python 经典面试题 21 道【不可错过】

    2023-10-29 04:56:20
  • Python3中详解fabfile的编写

    2022-08-09 14:33:08
  • JS与CSS合并写在同一份文件

    2010-01-23 12:33:00
  • Python正则替换字符串函数re.sub用法示例

    2021-03-04 17:25:42
  • mysql 5.7.14 下载安装配置方法图文教程

    2024-01-21 00:12:58
  • plt.figure()参数使用详解及运行演示

    2021-11-01 04:52:19
  • MySQL中几种数据统计查询的基本使用教程

    2024-01-13 22:29:56
  • YOLOv5车牌识别实战教程(一)引言与准备工作

    2021-06-20 09:01:06
  • 一文彻底理解JS回调函数

    2024-04-10 10:52:10
  • python有几个版本

    2021-09-12 00:44:30
  • golang值类型转换成[]uint8类型的操作

    2024-04-25 15:04:52
  • vscode 左侧扩展活动栏内容消失的问题及解决方法

    2022-01-12 04:26:35
  • JavaScript字符串对象substr方法入门实例(用于截取字符串)

    2024-06-05 09:53:44
  • Python 格式化输出字符串的方法(输出字符串+数字的几种方法)

    2021-05-03 21:25:00
  • 将字典转换为DataFrame并进行频次统计的方法

    2023-11-21 05:37:30
  • Python简单几步画个钻石戒指

    2023-04-26 13:59:27
  • 浅析python3中的os.path.dirname(__file__)的使用

    2021-11-10 04:35:23
  • Python爬虫爬取百度搜索内容代码实例

    2022-06-23 02:21:11
  • asp之家 网络编程 m.aspxhome.com