Python时间、日期、时间戳之间的转换
一、字符串与为时间字符串之间的互相转换
方法:time模块下的strptime方法
a = "2012-11-11 23:40:00"
# 字符串转换为时间字符串
import time
timeArray = time.strptime(a, "%Y-%m-%d %H:%M:%S") # 时间字符串转换为字符串
b = time.strftime("%Y/%m/%d %H:%M:%S", timeArray)
print(type(b))# <class 'str'>
二、将字符串的时间转换为时间戳
方法:字符串 --> 时间字符串 --> 时间戳
a = "2013-10-10 23:40:00"
# 将其转换为时间数组
import time
timeArray = time.strptime(a, "%Y-%m-%d %H:%M:%S")
# 转换为时间戳:
timeStamp = int(time.mktime(timeArray))
print(timeStamp)#
三、得到时间戳(10位和13位)
import time
t = time.time()
print(t) # 1436428326.207596
t_10 = int(t)# 10位时间戳
t_13 = int(round(time.time() * 1000))# 13位时间戳
print(t_10)#
print(t_13)#
四、将时间戳转换为时间格式的字符串
方法一:利用localtime()转换为时间数组,然后格式化为需要的格式
timeStamp = 1381419600# 10位时间戳
# timeStamp_13 = 1381419600234# 13位时间戳
timeArray = time.localtime(timeStamp)# timeStamp_13 / 1000
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
print(otherStyletime)# "2013-10-10 23:40:00"(str)
方法二、利用datetime模块下的utcfromtimestamp方法
import datetime
timeStamp = 1381419600
dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
otherStyleTime = dateArray.strftime("%Y-%m-%d %H:%M:%S")
print(otherStyletime) # "2013-10-10 23:40:00"
五、时间字符串转换为时间戳
方法:利用time模块的mktime方法
import time
import datetime
# 先获得时间数组格式的日期
test_date = datetime.datetime.now()
# 转换为时间戳:
timeStamp = int(time.mktime(test_date.timetuple()))
六、时间字符串加减日期
方法:利用datetime模块下的timedelta方法
import time
import datetime
# 先获得时间数组格式的日期
test_datetime = datetime.datetime.now()
threeDayAgo = (test_datetime - datetime.timedelta(days = 3))# 3天前
# 注:timedelta()的参数有:days,hours,seconds,microseconds
七、获取 UTC 时间戳
import calendar
calendar.timegm(datetime.datetime.utcnow().timetuple())
八、python 格式化时间含中文报错 UnicodeEncodeError: 'locale' codec can't encode character '\u5e74' in position 2: Illegal byte sequence'
import time
print(time.strftime(u'%Y年%m月%d日',time.localtime(time.time()))) # 执行上面代码会报错 UnicodeEncodeError: 'locale' codec can't encode character '\u5e74' in position 2: Illegal byte sequence # 解决方式:
time.strftime('%Y{y}%m{m}%d{d}').format(y='年',m='月',d='日')
Python时间、日期、时间戳之间的转换的更多相关文章
- python—时间与时间戳之间的转换
python-时间与时间戳之间的转换 对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运算,此时就需要对两种形式进行转换,在Python中,转换时需要用到time模块 ...
- python 时间与时间戳之间的转换
https://blog.csdn.net/kl28978113/article/details/79271518 对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运 ...
- python——时间与时间戳之间的转换
http://blog.csdn.net/google19890102/article/details/51355282
- js时间和时间戳之间如何转换(汇总)
js时间和时间戳之间如何转换(汇总) 一.总结 一句话总结: 1.js中通过new Date()来获取时间对象, 2.这个时间对象可以通过getTime()方法获取时间戳, 3.也可以通过getYea ...
- 【python-时间戳】时间与时间戳之间的转换
对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运算,此时就需要对两种形式进行转换,在Python中,转换时需要用到time模块,具体的操作有如下的几种: 将时间转换 ...
- js时间与时间戳之间的转换操作,返回天、小时、分,全家桶
1.将时间戳转换成时间 var formatDate = function(d) { var now = new Date(d); var year = now.getFullYear(); var ...
- python时间日期字符串各种
python时间日期字符串各种 第一种 字符串转换成各种日期 time 库 # -*- coding: utf-8 -*- import time, datetime # 字符类型的时间 tss1 = ...
- DB2中字符、数字和日期类型之间的转换
DB2中字符.数字和日期类型之间的转换 一般我们在使用DB2或Oracle的过程中,经常会在数字<->字符<->日期三种类 型之间做转换,那么在DB2和Oracle中,他们分别 ...
- Python数字与字符之间的转换
Python数字与字符之间的转换 命令 意义 int(x [,base ]) 将x转换为一个整数 long(x [,base ]) 将x转换为一个长整数 float(x ) 将x转换到一个浮点数 co ...
随机推荐
- linux ls命令教程,ls命令怎么用,全部招数都教你
linux ls命令的用法大全 学习linux这么久了,最常用的命令莫属 ls命令了,今天就总结下ls命令的用法与经验技巧. ls命令按文件大小查看文件 a.降序:ls -lsh moudae ...
- MySQL InnoDB 引擎的持久性与性能
MySQL 事务的 ACID 特性中,D 代表持久性(Durability):在使用 InnoDB 引擎时,当返回客户端一个成功完成事务的确认时, InnoDB 就会保证数据的一致性,即使该数据在此时 ...
- 2018年中国C++大会详细日程+报名
http://purecpp.org/detail?id=2050
- 干货 | Elasticsearch 集群健康值红色终极解决方案【转】
题记 Elasticsearch当清理缓存( echo 3 > /proc/sys/vm/drop_caches )的时候,出现 如下集群健康值:red,红色预警状态,同时部分分片都成为灰色. ...
- 在Java路上,我看过的一些书、源码和框架(转)
原文地址:http://www.jianshu.com/p/4a41ee88bd82 物有本末,事有终始,知所先后,则近道矣 面试经历 关于Java面试,你应该准备这些知识点关于Java面试,你应该准 ...
- Faster-RCNN 算法解读(转)
论文:<Faster R-CNN: Towards Real-Time ObjectDetection with Region Proposal Networks> 摘要:算法主要解决两个 ...
- Direct3D驱动类型(DRIVER_TYPE)介绍
之前部门老大叫我查找有关Direct3D使用软件渲染的方法,于是我找到了D3D驱动的类型,并整理如下 一.D3D驱动类型的句法 typedef enum D3D_DRIVER_TYPE { D3D_D ...
- Gephi学习笔记
使用gephi对图数据进行可视化操作,下面网址是gephi的说明文档 https://seinecle.github.io/gephi-tutorials/generated-pdf/semantic ...
- python让实例作用于for循环并当做list来使用
python如果想让一个类被用于for....in 循环,类型list和tuple那样,可以实现__iter__方法. 这个方法返回一个迭代对象,python的for循环就会不断调用该迭代对象的ne ...
- 我的预约订单页面List
<%@ page language="java" contentType="text/html;charset=UTF-8"%> <%@ ta ...