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 模块

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
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,有时需要与时间戳进行相互的运 ...
随机推荐
- Javaweb学习笔记——(一)——————进入html
1.html的简介 *什么是html? -HyperText Markup Language:超文本标记语言,网页语言 **超文本:超出文本的范畴,使用html可以轻松实现这些操作 **标记:html ...
- 第18月第25天 github下载单个文件夹 git命令
1. 用 SVN 即可. 举例说明: 譬如这个项目: Mooophy/Cpp-Primer · GitHub, 我只想看 ch03 文件夹的代码怎么办? 先打开 ch03, 其 URL 为: &quo ...
- linux下mysql 5.7.22 安装
二进制安装 1.下载https://dev.mysql.com/downloads/mysql/5.6.html#downloads 2.官方文档https://dev.mysql.com/doc/r ...
- mysql 索引无法使用问题
今天碰到一个问题,表中有一个索引不使用,怎么强制也没用 ,force index都没用, 后来才发现是类型不对, 比如索引字段是int,如果参数使用varchar,那么是无法使用索引的,参数类型最好统 ...
- SpringBoot几个重要的事件回调、监听机制
(1).需要配置在META-INF/Spring.factories 1.ApplicationContextInitializer // // Source code recreated from ...
- 【.net】获取网页CDM的下载链接的地址
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServi ...
- Nodejs+定时截图+发送邮件
功能 每天定时截图,并把截到的图片自动通过邮件发送. 说明 代码注释已经非常详细,就不多做说明,需要的朋友自己查看代码即可,主文件Mail.js,截图文件capturePart1.js,capture ...
- python中对列表和循环使用的小练习
#author devilf product_list = [ (), (), (), (), () ] shop_list = [] salary = input('pls enter your s ...
- windows2008r2系统破解登录密码方法
破解windows 2008 r2系统登录密码方法: 1.重启系统,使用windows2008r2安装光盘引导 按住shift+f10 2.切换到d:windows\system32目录(使用cmd. ...
- centos6环境创建局域网http方式的yum源
环境: yum服务器:centos 6.3 :192.168.8.20 yum源客户端:centos6.5 使用的主要rpm包来自centos6.5光盘 yum源服务器端配置: 1. 首先需要检查一下 ...