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

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
  • 12

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 转自:http://blog.csdn.net/google19890102/article/details/51355282

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

  1. python中unicode, hex, bin之间的转换

    python中unicode, hex, bin之间的转换 背景 在smb中有个feature change notify, 需要改动文件权限dacl,然后确认是否有收到notify.一直得不到这个d ...

  2. 转:Python常见字符编码及其之间的转换

    参考:Python常见字符编码 + Python常见字符编码间的转换 一.Python常见字符编码 字符编码的常用种类介绍 第一种:ASCII码 ASCII(American Standard Cod ...

  3. Python - 字符和字符值之间的转换

    字符和字符值之间的转换 Python中, 字符和字符值, 直接的转换, 包含ASCII码和字母之间的转换,Unicode码和数字之间的转换; 也可以使用map, 进行批量转换, 输出为集合, 使用jo ...

  4. Python str 与 bytes 类型 之间的转换

    bytes:字节数组,通常用它可以描述 “一个字符串”,只不过该字符串是  “bytes类型”,所以容易与str类型混淆,他们二者之间的转换: https://blog.csdn.net/lanchu ...

  5. js时间戳与日期格式之间的转换

    转换方法: var date = new Date(时间戳); //获取一个时间对象  注意:如果是uinx时间戳记得乘于1000. 比如php函数time()获得的时间戳就要乘于1000 //获取时 ...

  6. python中的字符数字之间的转换函数

    int(x [,base ])         将x转换为一个整数     long(x [,base ])        将x转换为一个长整数     float(x )               ...

  7. python中 字符 字典 列表之间的转换

    1 字典 转 字符 定义一个字典:dict = {'name': 'python', 'age': 7}字典转字符 可以使用str强制转换 如: str(dict) 此时dict的类型就是字符型了 2 ...

  8. Python中字符串/字典/json之间的转换

    import json #定义一个字典d1,字典是无序的 d1 = { "a": None, "b": False, "c": True, ...

  9. 【转】python中的字符数字之间的转换函数

    int(x [,base ])         将x转换为一个整数     long(x [,base ])        将x转换为一个长整数     float(x )               ...

  10. Python—IP地址与整数之间的转换

    1. 将整数转换成IP: 思路:将整数转换成无符号32位的二进制,再8位进行分割,每8位转换成十进制即可. 方法一:#!usr/bin/python 2 #encoding=utf-8 3 #1. 将 ...

随机推荐

  1. flask 需要下载的包

    Flask 需要下载的包1.pip install flask2.pip install flask-script3.pip install flask-sqlalchemy4.pip install ...

  2. mysql判断表里面一个逗号分隔的字符串是否包含单个字符串、查询结果用逗号分隔

    1.mysql判断表里面一个逗号分隔的字符串是否包含单个字符串 : FIND_IN_SET select * from tablename where FIND_IN_SET(传的参数,匹配字段) 例 ...

  3. Python数据结构与算法相关问题与解决技巧

      1.如何在列表, 字典, 集合中根据条件筛选数据¶ In [1]: from random import randint In [2]: data = [randint(-10,10) for _ ...

  4. 定点CORDIC算法求所有三角函数及向量模的原理分析、硬件实现(FPGA)

    一.CORDIC算法 CORDIC(Coordinate Rotation DIgital Computer)是一种通过迭代对多种数学函数求值的方法,它可以对三角函数.双曲函数和平面旋转问题进行求解. ...

  5. 用eclipse创建动态web项目手动生成web.xml方法

    建一个web项目,后来在用到web.xml文件时,才发现项目创建时没有自动创建web.xml文件. 在创建的项目上单击右键,然后单击java EE Tools下的用红线圈住的地方,然后查看你的WEB- ...

  6. java中mysql查询报错java.sql.SQLException: Before start of result set

    异常:java.sql.SQLException: Before start of result set 解决方法:使用rs.getString();前一定要加上rs.next(); sm = con ...

  7. pwnable.kr-input-witeup

    查看代码,有5个stage,一一解决掉就能愉快看到flag了. 第一个stage: 解决方案: 第二个stage: 解决方案: 使用了os.pipe()函数功能,os.pipe()用于创建一个管道,返 ...

  8. ELK-Elasticsearch 安装启动

    系统版本:Centos7 Elasticsearch:5.3.1 1:关闭SELinux [root@es local]# sed -i 's/SELINUX=enforcing/SELINUX=di ...

  9. java_30对文件的操作

    1.导包 导commons-io-2.4.jar包 2. public class Demo1Commons { public static void main(String[] args) { fu ...

  10. CentOS-07安装Redis学习笔记

    CentOS-07安装Redis 下载 http://download.redis.io/releases/redis-3.0.0.tar.gz 安装第一步:将下载的Redis源码包上传大奥Linux ...