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模块 ...
随机推荐
- log4j与commons-logging,slf4j的关系
前面有一篇日志中简单的介绍了 log4j,同时也介绍了它与commons-logging的关系,但是突然冒出来一个slf4j,并且slf4j有取代commons-logging的趋势,所以,我们可以推 ...
- Android使用AsyncTask实现可以断点续传的DownloadManager功能
http://www.it165.net/pro/html/201211/4210.html 最近做项目卡壳了,要做个Android的应用市场,其他方面都还好说,唯独这个下载管理算是给我难住了,究其原 ...
- mac 下的 top 命令
mac 下的 top 命令 文章目录 以前只是在 linux 机器上使用 top 命令.常用的快键键是: p 键 - 按 cpu 使用率排序 m 键 - 按内存使用量排序 这 2 个快捷键在 mac ...
- python表达式
算术表达式: 地板除: >>> 10 // 3 3>>> 5 // 2 2>>> 5 // 31 取余: >>> 10 % 31 ...
- android之Chronometer
首先定义activity_main.xml文件 代码如下: <LinearLayout xmlns:android="http://schemas.android.com/apk/re ...
- dig理解DNS的解析过程 - 阿权的书房
关于DNS的常识,可以阅读附录的一些参考资料.本文旨在尝试举例用dig命令理解这个过程,并非权威知识,仅供参考.测试域名为阿权的书房的域名 www.aslibra.com 和 www.163.com. ...
- javaWeb应用部署结构浅析
要成功部署一个Web应用,则必须遵循以下标准(参考)目录结构. 2.目录说明 1)WEB-INF目录:必须直接放在Web应用上下文之下(即一级目录). 2)class目录:必须直接放在WEB-INF目 ...
- 暑假训练round2 D: 好序列(Manacher)
Problem 1061: 好序列 Time Limits: 1000 MS Memory Limits: 65536 KB 64-bit interger IO format: %lld ...
- c#是否参入中间变量交换变量的几种方法
大家很熟悉知道,交换变量经常的使用的一种方法是使用第三个变量,也符合正常人的思维逻辑,但是还有其他的一些方法来实现,但是有点“偏门”,记住就好了.下面就列举这几种方法. 第一种方法,会用到参数的方法再 ...
- x86_64编译JPEG遇到Invalid configuration `x86_64-unknown-linux-gnu'
把 /usr/share/libtool/config/config.guess 覆盖到相关软件自带的config.guess 把 /usr/share/libtool/config/config ...