文章目录

  • 将timestamp转换为时间
  • 将时间转换为timestamp
  • 总结
  • 附录

将timestamp转换为时间

我们可以用time模块获取当前的时间戳

>>> import time
>>> time.time()
1600881396.692874

我们让t = 1600881396.692874,然后可以使用datetime模块将时间戳转换为日期时间

>>> from datetime import datetime
>>> datetime.fromtimestamp(t)
datetime.datetime(2020, 9, 24, 1, 16, 36, 692874)

将时间转换为timestamp

我们可以用datetime模块获取当前日期时间

>>> import datetime
>>> datetime.datetime.now()
datetime.datetime(2020, 9, 24, 1, 16, 36, 692874)

我们让n = datetime.datetime.now(),然后使用timestamp()方法转换为时间戳

>>> n.timestamp()
1600881396.692874

总结

#mermaid-svg-GN7syKsB4mXP7zNp {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-GN7syKsB4mXP7zNp .error-icon{fill:#552222;}#mermaid-svg-GN7syKsB4mXP7zNp .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-GN7syKsB4mXP7zNp .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-GN7syKsB4mXP7zNp .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-GN7syKsB4mXP7zNp .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-GN7syKsB4mXP7zNp .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-GN7syKsB4mXP7zNp .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-GN7syKsB4mXP7zNp .marker{fill:#333333;stroke:#333333;}#mermaid-svg-GN7syKsB4mXP7zNp .marker.cross{stroke:#333333;}#mermaid-svg-GN7syKsB4mXP7zNp svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-GN7syKsB4mXP7zNp .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-GN7syKsB4mXP7zNp .cluster-label text{fill:#333;}#mermaid-svg-GN7syKsB4mXP7zNp .cluster-label span{color:#333;}#mermaid-svg-GN7syKsB4mXP7zNp .label text,#mermaid-svg-GN7syKsB4mXP7zNp span{fill:#333;color:#333;}#mermaid-svg-GN7syKsB4mXP7zNp .node rect,#mermaid-svg-GN7syKsB4mXP7zNp .node circle,#mermaid-svg-GN7syKsB4mXP7zNp .node ellipse,#mermaid-svg-GN7syKsB4mXP7zNp .node polygon,#mermaid-svg-GN7syKsB4mXP7zNp .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-GN7syKsB4mXP7zNp .node .label{text-align:center;}#mermaid-svg-GN7syKsB4mXP7zNp .node.clickable{cursor:pointer;}#mermaid-svg-GN7syKsB4mXP7zNp .arrowheadPath{fill:#333333;}#mermaid-svg-GN7syKsB4mXP7zNp .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-GN7syKsB4mXP7zNp .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-GN7syKsB4mXP7zNp .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-GN7syKsB4mXP7zNp .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-GN7syKsB4mXP7zNp .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-GN7syKsB4mXP7zNp .cluster text{fill:#333;}#mermaid-svg-GN7syKsB4mXP7zNp .cluster span{color:#333;}#mermaid-svg-GN7syKsB4mXP7zNp div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-GN7syKsB4mXP7zNp :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}

datetime.datetime.fromtimestamp
n.timestamp
时间戳
日期时间

附录

字符与日期相互转换参照

python中日期字符串与datetime类型的相互转换

Python中timestamp时间戳和日期时间的转换相关推荐

  1. 如何在python中找到两个日期时间对象之间的时差?

    本文翻译自:How do I find the time difference between two datetime objects in python? 如何分辨两个datetime对象之间的时 ...

  2. 时间戳与日期时间的转换

    时间戳:2022-11-30 12:00:00.0,时间戳的getTime方法得到的一个long类型用来计算 yyyy-MM-dd HH:mm:ss  HH大写时为24小时,hh小写时为12小时 文章 ...

  3. 如何在python中构造时间戳参数

    1. 目的&思路 本次要构造的时间戳,主要有2个用途: headers中需要传当前时间对应的13位(毫秒级)时间戳 查询获取某一时间段内的数据(如30天前-当前时间) 接下来要做的工作: 获取 ...

  4. php date hi,php中时间戳和日期格式的转换

    原文:php中时间戳和日期格式的转换 一,PHP时间戳函数获取指定日期的unix时间戳 strtotime("2009-1-22″) 示例如下: echo strtotime("2 ...

  5. 如何在python中识别未知的日期时区时区

    本文翻译自:How to make an unaware datetime timezone aware in python What I need to do 我需要做什么 I have a tim ...

  6. Python中的时间戳具体是什么

    Python中的时间戳具体是什么 时间戳介绍 python表示 时间戳介绍 时间戳在python中时很常见的一种表示方式,但时间戳到底是什么?它数字表示的含义是什么?时间戳相隔1又有什么 意义呢? 时 ...

  7. JS获取当前时间戳与时间戳转日期时间格式

    一.获取当前时间的时间戳(三种方式) const t1 = new Date().valueOf() // 第一种,推荐 const t2 = new Date().getTime() // 第二种, ...

  8. js 时间戳与日期时间的相互转换

    js 时间戳与日期时间的相互转换 时间戳:指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总毫秒数. 1.获取时间戳 1)获取当前时 ...

  9. ios时间戳和日期的一些转换,如具体时间,年龄,星座等等

    1. ///将时间戳转为日期时间/// @param timestamp 时间戳/// @param dateFormat 日期样式 YYYY-MM-dd HH:mm:ss+ (NSString *) ...

最新文章

  1. (转)Unity3D - 性能优化之Draw Call
  2. Android--开发资源管理器/优化ListView显示列表方法
  3. rhel6多台主机的HA集群,并实现增加仲裁盘和共享存储
  4. python列表操作程序_Python列表操作,比较常见的10个问题
  5. Legacy(线段树优化建边跑Dijkstra)
  6. 移动端video播放时不弹出页面层
  7. 为何boss上的HR要了简历就不回复了?
  8. 让程序员设计界面的后果
  9. jquery.cookie.js
  10. oracle中between
  11. java界面编程(9) ------ 列表框
  12. java date timezoneoffset_Java TimeZone getOffset()用法及代码示例
  13. linux系统配置设置网络端口映射,Linux 配置端口映射
  14. 简述mysql实现递归查询的方法
  15. Google 广告考试试题
  16. (每日一练python)有效的数独
  17. CSP模拟试题——2020
  18. 新手学平面设计都会遇到哪些问题
  19. 小红书品牌营销白皮书(2021半年报)
  20. asp.net获取URL和IP地址(转)

热门文章

  1. ai不同形状的拼版插件_Illustrator(AI)自动拼版脚本插件
  2. CG学习记录(四)函数
  3. 门禁系统布线的十点注意事项
  4. linux下查看文件编码
  5. Redis学习(二)之 Redis Cluster集群
  6. 企业微信客户端开启调试模式
  7. Win7 的70个使用技巧
  8. JavaScript是单线程还是多线程?
  9. Microsoft Word 实用功能
  10. Java——继承——Extends