python时间戳、日期、时间转换
1.str转时间戳
# 字符类型的时间
tss1 = '2013-10-10 23:40:00'
# 转为时间数组
timeArray = time.strptime(tss1, "%Y-%m-%d %H:%M:%S")
print timeArray
# timeArray可以调用tm_year等
print timeArray.tm_year #
# 转为时间戳
timeStamp = int(time.mktime(timeArray))
print timeStamp # # 结果如下
time.struct_time(tm_year=2013, tm_mon=10, tm_mday=10, tm_hour=23, tm_min=40, tm_sec=0, tm_wday=3, tm_yday=283, tm_isdst=-1)
2013
1381419600
2.更改str类型日期的显示格式
tss2 = "2013-10-10 23:40:00"
# 转为数组
timeArray = time.strptime(tss2, "%Y-%m-%d %H:%M:%S")
# 转为其它显示格式
otherStyleTime = time.strftime("%Y/%m/%d %H:%M:%S", timeArray)
print otherStyleTime # 2013/10/10 23:40:00 tss3 = "2013/10/10 23:40:00"
timeArray = time.strptime(tss3, "%Y/%m/%d %H:%M:%S")
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
print otherStyleTime # 2013-10-10 23:40:00
3.时间戳转换为指定格式的日期
# 使用time
timeStamp = 1381419600
timeArray = time.localtime(timeStamp)
otherStyleTime = time.strftime("%Y--%m--%d %H:%M:%S", timeArray)
print otherStyleTime # 2013--10--10 23:40:00
# 使用datetime
timeStamp = 1381419600
dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
otherStyleTime = dateArray.strftime("%Y--%m--%d %H:%M:%S")
print otherStyleTime # 2013--10--10 15:40:00
4.获取当前时间并且用指定格式显示
# time获取当前时间戳
now = int(time.time()) #
timeArray = time.localtime(now)
print timeArray
otherStyleTime = time.strftime("%Y--%m--%d %H:%M:%S", timeArray)
print otherStyleTime # 结果如下
time.struct_time(tm_year=2018, tm_mon=8, tm_mday=11, tm_hour=9, tm_min=51, tm_sec=17, tm_wday=5, tm_yday=223, tm_isdst=0)
2018--08--11 09:51:17 # datetime获取当前时间,数组格式
now = datetime.datetime.now()
print now
otherStyleTime = now.strftime("%Y--%m--%d %H:%M:%S")
print otherStyleTime # 结果如下:
2018-08-11 09:51:17.362986
2018--08--11 09:51:17
5、时间加减
# 指定时间
now = '2019-4-5'
# 获取上周日期
d = datetime.datetime.strptime(now, '%Y-%m-%d')
delta = datetime.timedelta(days=7)
d1 = d - delta
last_week = d1.strftime('%Y-%m-%d')
print last_week # 结果如下
2019-03-29
python时间戳、日期、时间转换的更多相关文章
- SQL时间戳日期时间转换
将时间戳转换为日期格式:比如降1455504268→2016-02-15 10:44:28 select device.register_time a,FROM_UNIXTIME(device.reg ...
- Python学习---日期时间
在Python里面日期时间的功能主要由几个模块提供:time,calendar,datetime,date等 time主要用到的功能函数: #!/usr/bin/python3 # coding:ut ...
- Python实用日期时间处理方法汇总
这篇文章主要介绍了Python实用日期时间处理方法汇总,本文讲解了获取当前datetime.获取当天date.获取明天/前N天.获取当天开始和结束时间(00:00:00 23:59:59).获取两个d ...
- 分别用Excel和python进行日期格式转换成时间戳格式
最近在处理一份驾驶行为方面的数据,其中要用到时间戳,因此就在此与大家一同分享学习一下. 1.什么是时间戳? 时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01 ...
- python 之日期时间处理
##python时间操作一般使用time.datetime两个模块 对于time模块,时间的表示模式有3种1.时间戳:time.time()2.字符串: time.strftime('%Y%m%d') ...
- js中常用日期时间转换
常用日期时间处理插件:1. timeago.js处理几分钟之前 2. day.js 3. moment.js 注意: 1. 此处的标准时间格式为 2018-03-23 13:35:47 ...
- ORACLE函数之日期时间转换函数
1. TO_CHAR 语法:TO_CHAR(X [,format]) 说明:将X按format格式转换成字符串.X是一个日期或者数字.format是一个规定了X採用何种格式转换 ...
- 程序员常用6 个 Python 的日期时间库
内建的 datetime 模块 在跳转到其他库之前,让我们回顾一下如何使用 datetime 模块将日期字符串转换为 Python datetime 对象. 假设我们从 API 接受到一个日期字符串, ...
- Python基础 | 日期时间操作
目录 获取时间 时间映射 格式转换 字符串转日期 日期转字符串 unixtime 时间计算 时间偏移 时间差 "日期时间数据"作为三大基础数据类型之一,在数据分析中会经常遇到. 本 ...
- unix环境C编程之日期时间转换
1.理清概念 1.1.日历时间: 含义:国际标准时间1970年1月1日00:00:00以来经过的秒数. 数据类型:time_t.实际上是long的别名. 1.2.tm结构时间: 含义:结构 ...
随机推荐
- BZOJ2555 SubString【后缀自动机+LCT】
Description 懒得写背景了,给你一个字符串init,要求你支持两个操作 (1):在当前字符串的后面插入一个字符串 (2):询问字符串s在当前字符串中出现了几次?(作为连续子串) 你必须在线支 ...
- 剑指offer第五章
剑指offer第五章 1.数组中出现次数超过一半的数 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字. 例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组 ...
- 接口测试基础——第7篇 简单的Python知识普及(二)之装饰器
今天我们来学习python里面的“装饰器” 1.我在函数test运行前想先打印一些内容怎么办? def func(param): print u"打印的内容" para ...
- VS2013下的64位与32位程序配置
VS2013下的64位与32位程序配置 在Windows 7 64bit和Visual Studio 2013下生成64位程序. 新建一个Visual Studio Win32 Console项目 ...
- drone 学习四 几个有用的命令
1. 安装cli 工具 linux curl -L https://github.com/drone/drone-cli/releases/download/v0.8.5/drone_linux_am ...
- (转)Android DiskLruCache完全解析,硬盘缓存的最佳方案
摘自:http://blog.csdn.net/guolin_blog/article/details/28863651 转载请注明出处: http://blog.csdn.net/guolin_bl ...
- linq左连接
Table1和Table2连接,把Table1的全列出来 var tempData = from a in table1 join b in table2 on a.Id equals b.aId i ...
- Spring security 如何设置才能避免拦截到静态资源
问题:继承FilterSecurityInterceptor自定义Spring security拦截器,但是每次都拦截了css等静态资源,应该如何设置? @Override protected voi ...
- python接口自动化19-requests-toolbelt处理multipart/form-data
requests-toolbelt 1.官方文档地址:requests-toolbelt官方文档 2.环境安装 pip install requests-toolbelt multipart/form ...
- mysql索引之一:索引基础(B-Tree索引、哈希索引、聚簇索引、全文(Full-text)索引区别)(唯一索引、最左前缀索引、前缀索引、多列索引)
没有索引时mysql是如何查询到数据的 索引对查询的速度有着至关重要的影响,理解索引也是进行数据库性能调优的起点.考虑如下情况,假设数据库中一个表有10^6条记录,DBMS的页面大小为4K,并存储10 ...