对于时间数据,如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

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

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

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

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

  1. Python时间和时间戳互相转换

    # 将时间变成时间戳 def tranftimestamp(stringtime): try: return time.mktime(time.strptime(stringtime, "% ...

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

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

  3. Python 日期和时间戳的转换

    Python 日期和时间戳的转换 1. Python中处理时间的模块 Python中处理时间的模块有time.datetime和calendar. 在Python中表示时间的方式: 时间戳:10位整数 ...

  4. mysql小知识点汇总---(时间与时间戳的转换, 修改mysql用户名密码, navicate 导入sql文件报错 1153)

    1. 时间与时间戳的转换 1.1 时间戳转时间 FROM_UNIXTIME(add_time, '%Y-%m-%d') 1.2 时间转时间戳 UNIX_TIMESTAMP('2015-04-29') ...

  5. python 时间与时间戳之间的转换

    https://blog.csdn.net/kl28978113/article/details/79271518 对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运 ...

  6. js 时间与时间戳的转换

      一:时间转时间戳:javascript获得时间戳的方法有四种,都是通过实例化时间对象 new Date() 来进一步获取当前的时间戳 1.var timestamp1 = Date.parse(n ...

  7. Shell日期时间和时间戳的转换

    Gitlab的备份文件是以时间戳显示的,类似:1438624820_gitlab_backup.tar 为了更易于阅读,想把文件名转换成日期格式:2015-08-04_gitlab_backup.ta ...

  8. javascript 时间与时间戳的转换

    一:时间转时间戳:javascript获得时间戳的方法有五种,都是通过实例化时间对象 new Date() 来进一步获取当前的时间戳 1.var timestamp1 = Date.parse(new ...

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

    时间戳与时间之间的转换,需要一个中间过程,即将先将时间或时间戳先转为时间元组! 1.时间转时间戳: import datetime, time s = datetime.datetime(2016,6 ...

随机推荐

  1. KVC/KVO之KVO

    本章将分为三个部分: KVO是什么 KVO有什么用 KVO例子 KVO是什么 KVO,即NSKeyValueObserving,一个非正式的Protocol,提供一种机制来间接观察其他对象属性的变化. ...

  2. JQuery------鼠标双击时,不选中div里面的文字

    如图:(去掉选中文字的蓝色背景色) 代码: //方法一:<div class="test" onselectstart="return false" &g ...

  3. python线程池ThreadPoolExecutor用法

    线程池concurrent.futures.ThreadPoolExecutor模板 import time from concurrent.futures import ThreadPoolExec ...

  4. 160519、Oracle中将查询出的多条记录的某个字段拼接成一个字符串的方法

    with temp as( select 'China' nation ,'Guangzhou' city from dual union all select 'China' nation ,'Sh ...

  5. Struts2的CRUD操作

    Struts之CRUD 1何为CRUD:CRUD代表的是一个框架的Create(增),Read(读取),update(更新),Delete(删除) 2怎么做呢?? 其实Struts2的CRUD与现实的 ...

  6. 解决CMD编译中文乱码

    chcp 是MD DOS中的命令,用了显示或设置活动的代码编号的,用法查看当前编码格式(系统默认为:936  #936表示的是gb2312) chcp 修改当前编码格式(修改为utf-8) chcp ...

  7. Servlet------>jsp jstl核心标签库

    这里不需要刻意记,在jar里,c.tld文件都有,可以自己找源码看

  8. iOS 常用框架介绍

    iOS框架介绍      Cocoa Touch   GameKit  实现对游戏中心的支持,让用户能够在线共享他们的游戏相关的信息  iOS设备之间蓝牙数据传输   从iOS7开始过期   局域网游 ...

  9. w[wi].getAttribute('wattr')

    w 将数据库的不同数值寄存到自定义的不同属性上,避免通过寄存到同一id属性上的字符串处理.

  10. rrdtool ubuntu python snmpwalk

    rrdtool install: apt-get install libpango1.0-dev libxml2-dev wget https://packages.debian.org/wheezy ...