https://blog.csdn.net/kl28978113/article/details/79271518

对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运算,此时就需要对两种形式进行转换,在 Python 中,转换时需要用到time模块,具体的操作有如下的几种:

  • 将时间转换为时间戳
  • 重新格式化时间
  • 时间戳转换为时间
  • 获取当前时间及将其转换成时间戳

1、将时间转换成时间戳

将如上的时间2016-05-05 20:28:54转换成时间戳,具体的操作过程为:

  • 利用strptime()函数将时间转换成时间数组
  • 利用mktime()函数将时间数组转换成时间戳
#coding:UTF-8
import time dt = "2016-05-05 20:28:54" #转换成时间数组
timeArray = time.strptime(dt, "%Y-%m-%d %H:%M:%S")
#转换成时间戳
timestamp = time.mktime(timeArray) print timestamp
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

2、重新格式化时间

重新格式化时间需要以下的两个步骤:

  • 利用strptime()函数将时间转换成时间数组
  • 利用strftime()函数重新格式化时间
#coding:UTF-8
import time dt = "2016-05-05 20:28:54" #转换成时间数组
timeArray = time.strptime(dt, "%Y-%m-%d %H:%M:%S")
#转换成新的时间格式(20160505-20:28:54)
dt_new = time.strftime("%Y%m%d-%H:%M:%S",timeArray) print dt_new
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

3、将时间戳转换成时间

在时间戳转换成时间中,首先需要将时间戳转换成 localtime,再转换成时间的具体格式:

  • 利用localtime()函数将时间戳转化成 localtime 的格式
  • 利用strftime()函数重新格式化时间
#coding:UTF-8
import time timestamp = 1462451334 #转换成localtime
time_local = time.localtime(timestamp)
#转换成新的时间格式(2016-05-05 20:28:54)
dt = time.strftime("%Y-%m-%d %H:%M:%S",time_local) print dt
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

4、按指定的格式获取当前时间

利用time()获取当前时间,再利用localtime()函数转换为 localtime,最后利用strftime()函数重新格式化时间。

#coding:UTF-8
import time #获取当前时间
time_now = int(time.time())
#转换成localtime
time_local = time.localtime(time_now)
#转换成新的时间格式(2016-05-09 18:59:20)
dt = time.strftime("%Y-%m-%d %H:%M:%S",time_local) print dt
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

转载:http://blog.csdn.net/google19890102/article/details/51355282

python 时间与时间戳之间的转换的更多相关文章

  1. python—时间与时间戳之间的转换

    python-时间与时间戳之间的转换 对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运算,此时就需要对两种形式进行转换,在Python中,转换时需要用到time模块 ...

  2. python——时间与时间戳之间的转换

    http://blog.csdn.net/google19890102/article/details/51355282

  3. js时间和时间戳之间如何转换(汇总)

    js时间和时间戳之间如何转换(汇总) 一.总结 一句话总结: 1.js中通过new Date()来获取时间对象, 2.这个时间对象可以通过getTime()方法获取时间戳, 3.也可以通过getYea ...

  4. 【python-时间戳】时间与时间戳之间的转换

    对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运算,此时就需要对两种形式进行转换,在Python中,转换时需要用到time模块,具体的操作有如下的几种: 将时间转换 ...

  5. js时间与时间戳之间的转换操作,返回天、小时、分,全家桶

    1.将时间戳转换成时间 var formatDate = function(d) {  var now = new Date(d); var year = now.getFullYear(); var ...

  6. Python数字与字符之间的转换

    Python数字与字符之间的转换 命令 意义 int(x [,base ]) 将x转换为一个整数 long(x [,base ]) 将x转换为一个长整数 float(x ) 将x转换到一个浮点数 co ...

  7. Python时间、日期、时间戳之间的转换

    一.字符串与为时间字符串之间的互相转换 方法:time模块下的strptime方法 a = "2012-11-11 23:40:00" # 字符串转换为时间字符串 import t ...

  8. python 时间字符串和时间戳之间的转换

    https://blog.csdn.net/qq_37193537/article/details/78987949   1.将字符串的时间转换为时间戳    方法:        a = " ...

  9. python 时间和时间戳的转换

    对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运算,此时就需要对两种形式进行转换,在Python中,转换时需要用到time模块,具体的操作有如下的几种: 将时间转换 ...

随机推荐

  1. How to clone a brach from github

    git clone address.git -b brach_name destination_floder

  2. wait和waitpid

    当有多个子进程的SIGCHLD信号到达父进程的时候,如果父进程用wait等待,那么父进程在处理第一个达到的SIGCHLD信号的时候,其他的SIGCHLD信号被堵塞,而且信号不被缓存,这样就会导致信号丢 ...

  3. myeclipse 上安装 Maven3<转>

    环境准备: JDK 1.6 Maven 3.0.4 myeclipse 8.6.1 安装 Maven 之前要求先确定你的 JDK 已经安装配置完成.Maven是 Apache 下的一个项目,目前最新版 ...

  4. JQuery元素选择

    1.基本元素选择器 $(“p”) //选取<p>元素 $(“p.info”) //选取所有class=”info”的<p>元素 $(“p#demo”) //选取id=”demo ...

  5. android monkey app乱点测试

    Monkey是Android中的一个命令行工具 查看包名:查看电脑中某一位置的apk文件的包名:PC打开CMD-进入TMG目录-运行设备--查看包名aapt dump badging *.apk(ap ...

  6. STUN协议简析

    http://blog.csdn.net/mazidao2008/article/details/4934257 ——————————————————————————————————————————— ...

  7. 服务器的svnserver修改密码

    VisualSVN Server是一个集成的svn服务端工具,是一款svn服务端不可多得的好工具.可以先安装好VisualSVN Server后,运行VisualSVN Server Manger,然 ...

  8. phpstorm 中文版 支持BUG调试 IDE

    下载地址:http://dx2.7down.net/soft/P/phpstorm8_cn.zip

  9. MyBatis常用对象SqlSessionFactory和SqlSession介绍和运用

    学习框架一个比较好的路径阅读源码.本文介绍的SqlSessionFactory和SqlSession.可以通过了解SqlSessionFactory接口和SqlSession接口以及两个的实现类入手, ...

  10. 工业级别sd卡存贮slc mlc tlc

    slc mlc tlc SLC = Single-Level Cell ,即1bit/cell,速度快寿命长,价格超贵(约MLC 3倍以上的价格),约10万次擦写寿命 MLC = Multi-Leve ...