Python 时间获取
摘自:http://www.jb51.net/article/91365.htm
摘自:https://www.cnblogs.com/liuq/p/6211005.html
一、在python中,除了time模块外还有datetime模块,也可以方便的操作时间,比如用datetime模块来显示当前时间
>>> from datetime
import
datetime
>>> datetime.now().strftime(
'%Y-%m-%d %H:%M:%S'
)
'2016-07-21 19:49:15'
>>> datetime.now().isoformat()
'2016-07-21T19:56:46.744893'
>>> str(datetime.now())
'2016-07-21 19:48:37.436886'
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 模块
![](https://common.cnblogs.com/images/copycode.gif)
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
![](https://common.cnblogs.com/images/copycode.gif)
fromtimestamp(timestamp[, tz]):将时间戳转为当地的时间元组
utcfromtimestamp(timestamp):将时间戳转为UTC的时间元组。以北京为例:utc时间比北京当地时间少8个小时。
3. 时间差计算
1) 几天/周前
![](https://common.cnblogs.com/images/copycode.gif)
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)
![](https://common.cnblogs.com/images/copycode.gif)
2) 几天/周后
![](https://common.cnblogs.com/images/copycode.gif)
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)
![](https://common.cnblogs.com/images/copycode.gif)
注意:没有months和years
3)时间差
![](https://common.cnblogs.com/images/copycode.gif)
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 毫秒数
![](https://common.cnblogs.com/images/copycode.gif)
注意:没有分钟
4. 任意时间字符串转换时间对象
![](https://common.cnblogs.com/images/copycode.gif)
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
today = datetime.date.today()
yesterday = today - datetime.timedelta(days=10)
today = today - datetime.timedelta(days=0)
date_from = str(yesterday)+' 00:00:00'
date_till = str(today)+' 23:59:59'
unix_from = int(time.mktime(time.strptime(date_from, '%Y-%m-%d %H:%M:%S')))#转换为时间戳
unix_till = int(time.mktime(time.strptime(date_till, '%Y-%m-%d %H:%M:%S')))#转换为时间戳
Python 时间获取的更多相关文章
- Python时间获取详解,Django获取时间详解,模板中获取时间详解(navie时间和aware时间)
1.Python获取到的时间 import pytz from datetime import datetime now = datetime.now() # 这个时间为navie时间(自己不知道自己 ...
- Python时间获取及转换知识汇总
时间处理是我们日常开发中最最常见的需求,例如:获取当前datetime.获取当天date.获取明天/前N天.获取当天开始和结束时间(00:00:00 23:59:59).获取两个datetime的时间 ...
- 浅谈Python时间模块
浅谈Python时间模块 今天简单总结了一下Python处理时间和日期方面的模块,主要就是datetime.time.calendar三个模块的使用.希望这篇文章对于学习Python的朋友们有所帮助 ...
- python时间处理之datetime
python时间处理之datetime 标签: pythondateimportstringc 2012-09-12 23:21 20910人阅读 评论(0) 收藏 举报 分类: Python系列( ...
- python 单例模式获取IP代理
python 单例模式获取IP代理 tags:python python单例模式 python获取ip代理 引言:最近在学习python,先说一下我学Python得原因,一个是因为它足够好用,完成同样 ...
- 基于Python Shell获取hostname和fqdn释疑
一直以来被Linux的hostname和fqdn(Fully Qualified Domain Name)困惑了好久,今天专门抽时间把它们的使用细节弄清了. 一.设置hostname/fqdn 在Li ...
- python时间模块详解(time模块)
time 模块 -- 时间获取和转换 time 模块提供各种时间相关的功能 在 Python 中,与时间处理有关的模块包括:time,datetime 以及 calendar 必要说明: 虽然这个模块 ...
- Python时间日期格式化之time与datetime模块总结
1 引言 在实际开发过程中,我们经常会用到日期或者时间,那么在Python中我们怎么获取时间,以及如何将时间转换为我们需要的格式呢?在之前的开发中,也曾遇到time.datetime等模块下的不同函数 ...
- python 时间与时间戳之间的转换
https://blog.csdn.net/kl28978113/article/details/79271518 对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运 ...
随机推荐
- JAVA实现具有迭代器的线性表(顺序表)
1,先了解下JAVA类库中的迭代器:JAVA提供了两种基本类型的迭代器,分别用两个接口来表示:Iterator<T>,ListIterator<T>.其中,Iterator&l ...
- Java——集合
Java的集合类是一种非常有用的工具类,用于存储多个对象.它是一个容器,可以把多个对象放到里面. Java集合分三种情况: Set:无序.不可重复 List:有序.可重复 Map:具有映射关系 Col ...
- 集大软件工程15级个人作业Week1
集大软件工程15级个人作业Week1 孙志威 201521123077 博客园主页 码云地址 阅读参考材料,并回答下面几个问题 (1)回想一下你初入大学时对网络工程专业的畅想 当初你是如何做出选择网络 ...
- .NET 单点登录开源项目
1. https://www.apereo.org/cas/client-integration 2.源码下载 https://wiki.jasig.org/display/CASC/.Net+Cas ...
- QMessageBox消息框
QMessageBox提供两套接口来实现,一种是static functions(静态方法调用),另外一种 the property-base API(基于属性的API) #需要 from PyQt5 ...
- linux bash的重定向
cnblogs原创 下面几种bash重定向各表示什么意思? find / -name passwd > /dev/null >& > /dev/null find / -na ...
- SpringBoot几个重要的事件回调、监听机制
(1).需要配置在META-INF/Spring.factories 1.ApplicationContextInitializer // // Source code recreated from ...
- KVM -> 虚拟机在线热添加技术_04
热添加技术 1.KVM在线热添加硬盘
- Expm 1_3 数组中逆序对个数问题
有一个数的序列A[1].A[2] .A[3] .…… .A[n],若i<j,并且A[i]>A[j],则称A[i]与A[j]构成了一个逆序对,设计算法求数列A中逆序对的个数. package ...
- Centos7 通过yum命令安装jdk1.8
直接安装,不看原因 yum install java-1.8.0-openjdk* -y 1 分割线上下之选一个看即可. —————————————华丽的分割线—————————————— 先查看系统 ...