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 ...
随机推荐
- Django Http请求生命周期
day54 请求响应Http 1.发送Http请求 2.服务器接收,根据请求头中的的url在路由关系表中进行匹配(从上到下) 3.匹配成功后,执行指定的views函数 4.业务处理 URL----&g ...
- C#指定长度截取字符串 并进行拼接。
需求:有一个字符串需要按照指定长度拆分出来,然后在增加一个字符串拼接上. /// <summary> /// 字符串截取并拼接 /// </summary> /// <p ...
- Nios II 程序固化(如何下载elf文件)
Nios II 程序固化(如何下载elf文件) 2018年10月15日 21:37:32 瓜儿不甜 阅读数:723 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog ...
- CAS单点登陆,URL多出个参数jsessionid导致登陆失败问题
目录: 1.定位问题 2.问题产生的原因 3.解决问题 一 定位问题 首先,如下图所示:输入到地址栏的地址被302重定向到单点登录地址,地址由Response Headers中的参数Location所 ...
- 修改npm安装的全局路径和配置环境变量的坑
修改npm安装的全局路径和配置环境变量的坑 转自:http://www.qdfuns.com/notes/30749/0f66fcf5e62eed010f744d0d4adaa870.html 我之前 ...
- Android 8 蓝牙打开过程
packages\apps\Settings\src\com\android\settings\bluetooth\BluetoothEnabler.java @Override public boo ...
- (转)x264的一些参数设置对编码效率的影响
转自:http://www.cnblogs.com/wainiwann/p/5647521.html i_luma_deadzone[0]和i_luma_deadzone[1]分别对应inter和in ...
- VMware ESXI添加第三方网卡驱动
VMware ESXI有两种方法添加第三方网卡驱动: 1.使用第三方工具 ESXI-Customizer.cmd工具可以将已经下载好的VMware ESXI.ISO镜像文件把下载好的驱动添加到里面,缺 ...
- Thread类的join()方法
public class Demo { /** * Thread类的join()方法 * -------------------------------- * 1)join() * 2)join(lo ...
- 虚拟机---vmmare15安装centos7.4
第一步:下载centos7的镜像iso文件:http://isoredirect.centos.org/centos/7/isos/x86_64/CentOS-7-x86_64-Everything- ...