Python处理时间 time && datetime 模块
Python处理时间 time && datetime 模块
个人整理,获取时间方式:
import datetime
import time #获取当前时间:Thu Nov 03 16:40:00 2016
print time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()) #获取当前时间:2016-11-03 16:40:00
print datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') #获取年,月,日:2016-11-03
print datetime.date.today() #获取当前时间:2016-11-03 16:43:14.550000
print datetime.datetime.now() #不加参数是00:00,参数days=1表示一天:1 day, 0:00:00
print datetime.timedelta(days=1) #获取昨天日期:2016-11-02
nowtime=datetime.date.today()
oldtime=datetime.timedelta(days=1)
print(nowtime-oldtime) #获取昨天的精确日期
oldtime=datetime.timedelta(days=1)
print (datetime.datetime.now() - oldtime)
python时间处理之time模块:
import time
【返回时间戳】
print(time.time()) 【返回当前时间】
print(time.ctime()) 【返回一天前的时间】
print(time.ctime(time.time()-86400)) 【函数返回time.struct_time类型的对象】
time_obj = time.gmtime()
print(time_obj)
结果:time.struct_time(tm_year=2016, tm_mon=7, tm_mday=27, tm_hour=8, tm_min=52, tm_sec=26, tm_wday=2, tm_yday=209, tm_isdst=0)
格式化输出:
print(time_obj.tm_year,time_obj.tm_mon,time_obj.tm_mday) print("{year}-{month}".format(year=time_obj.tm_year,month=time_obj.tm_mon)) 【以time.struct_time类型,打印本地时间】
print(time.localtime()) 【转换成时间戳】
time_obj = time.gmtime()
print(time.mktime(time_obj)) 【延时2秒】
time.sleep(2) 【打印UTC,世界标准时间,北京时区是东八区,领先UTC八个小时】
print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime())) 【本地时间】
print(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())) 【把time.struct_time类型时间,转换成时间戳】
tm = time.strptime("2016-05-6 15:06:33","%Y-%m-%d %H:%M:%S")
print(tm)
print(time.mktime(tm))
python时间处理之datetime模块:
import datetime 【打印当前,年,月,日】
print(datetime.date.today()) 【打印当前时间,精确到微秒】
current_time = datetime.datetime.now()
print(current_time) 【转成time.struct_time格式时间】
current_time = datetime.datetime.now()
print(current_time.timetuple()) 【加十天】
print(datetime.datetime.now() +datetime.timedelta(days=10))
【减十天】
print(datetime.datetime.now() +datetime.timedelta(days=-10))
【减十个小时】
print(datetime.datetime.now() +datetime.timedelta(hours=-10))
【加120s】
print(datetime.datetime.now() +datetime.timedelta(seconds=120)) 【替换成指定的时间】
cr_time = datetime.datetime.now()
print(cr_time.replace(2014,9,12))
结果:2014-09-12 17:28:17.522893 【格式化输出】
print(datetime.datetime.strptime("21/11/06 16:30","%d/%m/%y %H:%M")) 【替换成指定时间后,类型是<class 'datetime.datetime'>】
current_time = datetime.datetime.now()
time_obj = current_time.replace(2015,5)
print(time_obj,type(time_obj))
结果:2015-05-27 17:34:13.350245 <class 'datetime.datetime'> 【对比时间大小,取指定时间范围使用】
current_time = datetime.datetime.now()
time_obj = current_time.replace(2015,5)
print(current_time>time_obj)
获取昨天的日期
import datetime
def getYesterday():
today=datetime.date.today()
oneday=datetime.timedelta(days=1)
yesterday=today-oneday
return yesterday # 输出
print(getYesterday())
Python处理时间 time && datetime 模块的更多相关文章
- python time 和 datetime模块
time模块 时间相关的操作,时间有三种表示方式: 时间戳 1970年1月1日之后的秒,即:time.time() 格式化的字符串 2014-11-11 11:11, ...
- python time 和 datetime 模块
时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.我们运行“type(time.time())”,返回的是float类型. 格式化的时间字 ...
- python time、datetime模块
时间的三种格式:1)时间戳 2)格式化的时间字符串 3)元组(struct_time):time.struct_time(tm_year=1970, tm_mon=5, tm_mday=23, tm_ ...
- python time 和 datetime 模块的简介
时间处理 time 和 datetime import timeimport datetimeprint time.time() #时间戳显示为1508228106.49print time.strf ...
- python内建datetime模块
datetime 获取当前日期和时间 from datetime import datetime now = datetime.now() print(now) datetime转换为timestam ...
- python使用datetime模块计算各种时间间隔的方法
python使用datetime模块计算各种时间间隔的方法 本文实例讲述了python使用datetime模块计算各种时间间隔的方法.分享给大家供大家参考.具体分析如下: python中通过datet ...
- python的datetime模块处理时间
python的datetime模块主要用来处理时间,里面包含很多类,包括timedelay,date,time,datetime等 开发中经常会用到模块里面的datetime类,这是一个表示日期时间的 ...
- python处理时间--- datetime模块
1 Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供的接口与C标准库time.h基本一致.相比于tim ...
- python中datetime模块
Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供 的接口与C标准库time.h基本一致.相比于time模块 ...
随机推荐
- elasticsearch2.2 集群搭建各种坑
目前生产环境的es版本是1.0版本,需要升级到最新的2.2版本,于是在测试环境进行部署集群测试,在测试过程中遇到的坑相当多,下面详细介绍下. 1. 版本升级到2.2后,必须建一个单 ...
- Delphi中控制Excel(转载)
用Delphi从数据库中取得资料,然后导出到Excel中做成报表是个不错的选择,因为Excel强大的报表功能那可是没话说前提Delphi中要 uses comobj;var Excel:Variant ...
- 理解with(nolock)(转载)
本文导读:要 提升SQL的查询效能,一般来说大家会以建立索引(index)为第一考虑.其实除了index的建立之外,当我们在下SQL Command时,在语法中加一段WITH (NOLOCK)可以改善 ...
- Hibernate工作原理及为什么要用?
Hibernate工作原理及为什么要用? 原理:1.通过Configuration().configure();读取并解析hibernate.cfg.xml配置文件2.由hibernate.cfg.x ...
- ACM对时间掌控力和日积月累的习惯的意义
马云说,要想创业成功,不是要知道现在什么东西最火,而是要清楚的知道十年以后什么东西最火.这就意味着,你对时间掌控力,至少要有十年. 但是仔细回想一下自己的学生时代,自己对时间的把握是怎样的?有些人只能 ...
- html5文章 -- HTML5开发实例-网易微博手机Web App开发过程
HTML5在国内外越来越受到互联网开发团队的青睐.国外,谷歌兴致勃勃地开发Chrome Web Store,微软发布了支持使用HTML5技术开发的“Irish Spring”主题网站,诺基亚斥巨资购得 ...
- java如何产生随机数
一.java如何产生随机数? 1.打开eclipse 2.新建java项目,例如取名为“suijishu”点击完成 3.新建一个类进行测试 4.首先要在头部插入一个包 输入import java.ut ...
- Acronis True Image Home 2011 PXE服务器配置_qxxz_新浪博客
想实现网络启动,并且Acronis启动菜单中带有Acronis True Image Home,需要安装以下软件: 1.安装Acronis True Image Home 2011及plush pac ...
- 在 MySQL 中查找含有目标字段的表
要查询数据库中哪些表含有目标字段,可以使用语句: SELECT TABLE_SCHEMA,TABLE_NAME FROM information_schema.`COLUMNS` WHERE COLU ...
- Web 在线文件管理器学习笔记与总结(4)查看文件内容
② 查看文件内容 a.通过 file_get_contents($filename) 得到文件内容 b.通过 highlight_string($string) 或者 highlight_file($ ...