Python 之 时间字符串、时间戳、时间差、任意时间字符串转换时间对象
1. 时间字符串 --> 时间戳
1) time 模块
timestring = '2016-12-21 10:22:56'
print time.mktime(time.strptime(timestring, '%Y-%m-%d %H:%M:%S')) # 1482286976.0
time.mktime() 与 time.localtime() 互为还原函数。
time.mktime(timetuple) :将时间元组转换成时间戳
time.localtime([timestamp]):将时间戳转会为时间元组
2) datetime 模块
在这里没有找到,似乎只有 time 模块能获取时间戳
2. 时间戳 --> 时间字符串
1) time 模块
timestamp = time.time()
timestruct = time.localtime(timestamp)
print time.strftime('%Y-%m-%d %H:%M:%S', timestruct) # 2016-12-22 10:49:57
2) datetime 模块
import datetime
timestamp = 1482374997.55
datetime_struct = datetime.datetime.fromtimestamp(timestamp)
print datetime_struct.strftime('%Y-%m-%d %H:%M:%S') # 2016-12-22 10:49:57 datetime_struct = datetime.datetime.utcfromtimestamp(timestamp)
print datetime_struct.strftime('%Y-%m-%d %H:%M:%S') # 2016-12-22 02:49:57
fromtimestamp(timestamp[, tz]):将时间戳转为当地的时间元组
utcfromtimestamp(timestamp):将时间戳转为UTC的时间元组。以北京为例:utc时间比北京当地时间少8个小时。
3. 时间差计算
1) 几天/周前
import datetime now = datetime.datetime.now()
three_days_ago = now + datetime.timedelta(days=-3)
three_weeks_ago = now + datetime.timedelta(weeks=-3) print now # datetime.datetime(2016, 12, 22, 11, 24, 49, 987171)
print three_days_ago # datetime.datetime(2016, 12, 19, 11, 24, 49, 987171)
print three_weeks_ago # datetime.datetime(2016, 12, 1, 11, 24, 49, 987171)
2) 几天/周后
import datetime now = datetime.datetime.now()
three_days_later = now + datetime.timedelta(days=3)
three_weeks_later = now + datetime.timedelta(weeks=3) print now # datetime.datetime(2016, 12, 22, 11, 24, 49, 987171)
print three_days_later # datetime.datetime(2016, 12, 25, 11, 24, 49, 987171)
print three_weeks_later # datetime.datetime(2017, 1, 12, 11, 24, 49, 987171)
注意:没有months和years
3)时间差
import time
import datetime start = datetime.datetime.now()
time.sleep(30)
end = datetime.datetime.now() print (end-start).days # 0 天数
print (end-start).total_seconds() # 30.029522 精确秒数
print (end-start).seconds # 30 秒数
print (end-start).microseconds # 29522 毫秒数
注意:没有分钟
4. 任意时间字符串转换时间对象
import time
from dateutil import parser time_string = time.ctime() # 'Thu Dec 22 10:35:25 2016',这里可以是任意的时间格式
datetime_struct = parser.parse(time_string)
print type(datetime_struct) # <type 'datetime.datetime'>
print datetime_struct.strftime('%Y-%m-%d %H:%M:%S') # 2016-12-22 13:58:59
Python 之 时间字符串、时间戳、时间差、任意时间字符串转换时间对象的更多相关文章
- python(6)时间戳和北京时间互转,输出当前的时间和推到七天前的日期
项目发展的需要:(包含时间函数)time datetime 时间戳和北京时间互转 import time import datetime s = '2015-04-17 11:25:30' d = d ...
- Js获取当前的日期和时间以及时间戳转化为时间
/** *获取当前时间 *format=1精确到天 *format=2精确到分 */ function getCurrentDate(format) { var now = new Date(); v ...
- python 时间与时间戳之间的转换
https://blog.csdn.net/kl28978113/article/details/79271518 对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运 ...
- python 时间和时间戳的转换
对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运算,此时就需要对两种形式进行转换,在Python中,转换时需要用到time模块,具体的操作有如下的几种: 将时间转换 ...
- python—时间与时间戳之间的转换
python-时间与时间戳之间的转换 对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运算,此时就需要对两种形式进行转换,在Python中,转换时需要用到time模块 ...
- 【python-时间戳】时间与时间戳之间的转换
对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运算,此时就需要对两种形式进行转换,在Python中,转换时需要用到time模块,具体的操作有如下的几种: 将时间转换 ...
- utc时间、本地时间及时间戳转化
1.时间戳的概念 时间戳的定义请看百科unix时间戳,需要注意的时间戳为当前时刻减去UTC时间(1970.1.1)零点时刻的秒数差,与当前系统所处的时区无关,同一时刻不管在任何时区下得到的时间戳都是一 ...
- Shell初学(六)Linux Shell 时间运算以及时间差计算方法
Linux Shell 时间运算以及时间差计算方法 时间的加减,以及时间差的计算. 1. 时间加减 这里处理方法,是将基础的时间转变为时间戳,然后,需要增加或者改变时间,变成 秒. 如:1990-01 ...
- Java时间和时间戳的相互转换
时间转换为时间戳: /* * 将时间转换为时间戳 */ public static String dateToStamp(String s) throws ParseException{ String ...
随机推荐
- Intent中的四个重要属性——Action、Data、Category、Extras
Intent作为联系各Activity之间的纽带,其作用并不仅仅只限于简单的数据传递.通过其自带的属性,其实可以方便的完成很多较为复杂的操作.例如直接调用拨号功能.直接自动调用合适的程序打开不同类型的 ...
- 【转载】Spark性能优化指南——高级篇
前言 数据倾斜调优 调优概述 数据倾斜发生时的现象 数据倾斜发生的原理 如何定位导致数据倾斜的代码 查看导致数据倾斜的key的数据分布情况 数据倾斜的解决方案 解决方案一:使用Hive ETL预处理数 ...
- UICollectionViewCell选中高亮状态和UIButton的高亮状态和选中状态
UICollectionViewCell选中高亮状态 //设置点击高亮和非高亮效果! - (BOOL)collectionView:(UICollectionView *)collectionView ...
- Surprise团队第三周项目总结
Surprise团队第二周项目总结 项目进展 这周我们小组的项目在上周的基础上进行了补充,主要注重在注册登录界面的实现,以及关于数据库的一些学习. 在设计注册登录界面时,每一块的地方控件都不一样,比如 ...
- 使用lua实现一个简单的事件派发器
设计一个简单的事件派发器,个人觉得最重要的一点就是如何保证事件派发过程中,添加或删除同类事件,不影响事件迭代顺序和结果,只要解决这一点,其它都好办. 为了使用pairs遍历函数,重写了pairs(lu ...
- Eplan简单教程
鉴于AUTOCAD画电路图比较繁琐而且手动添加关联参考错误率较高,而EPLAN画电路图确实效率要高许多,也更规范.过年正好有点时间,把我这段时间学EPLAN的一些经验总结了一下,有兴趣可以看看,也省得 ...
- redis之(二十一)redis之深入理解Spring Redis的使用
关于spring redis框架的使用,网上的例子很多很多.但是在自己最近一段时间的使用中,发现这些教程都是入门教程,包括很多的使用方法,与spring redis丰富的api大相径庭,真是浪费了这么 ...
- Windows下利用虚拟机运行FSL 安装和配置
FSL是牛津大学FMRIB开发的用于分析功能磁共振影像的科研软件包. 运行要求:windows7或vista操作系统,10G硬盘空间,4G内存. 从FSL官网上下载Centos6的虚拟盘(FSLVm6 ...
- (转)Predictive learning vs. representation learning 预测学习 与 表示学习
Predictive learning vs. representation learning 预测学习 与 表示学习 When you take a machine learning class, ...
- sqlalchemy数据模型
sqlalchemy在python里作为orm还是比较有名气的,以下是建立的几个简单模型,完全可和flask的数据持久层分离. # coding: utf8 from sqlalchemy impor ...