python time与datetime.date/datetime模块
https://docs.python.org/3/library/datetime.html
1、用于日期比较大小的方法
| 方法名 | 方法说明 | 用法 |
|---|---|---|
| __eq__(…) | 等于(x==y) | x.__eq__(y) |
| __ge__(…) | 大于等于(x>=y) | x.__ge__(y) |
| __gt__(…) | 大于(x>y) | x.__gt__(y) |
| __le__(…) | 小于等于(x<=y) | x.__le__(y) |
| __lt__(…) | 小于(x | x.__lt__(y) |
| __ne__(…) | 不等于(x!=y) | x.__ne__(y) |
2、时间格式
python中时间日期格式化符号:
%y 两位数的年份表示(00-99)
%Y 四位数的年份表示(000-9999)
%m 月份(01-12)
%d 月内中的一天(0-31)
%H 24小时制小时数(0-23)
%I 12小时制小时数(01-12)
%M 分钟数(00=59)
%S 秒(00-59)
%a 本地简化星期名称
%A 本地完整星期名称
%b 本地简化的月份名称
%B 本地完整的月份名称
%c 本地相应的日期表示和时间表示
%j 年内的一天(001-366)
%p 本地A.M.或P.M.的等价符
%U 一年中的星期数(00-53)星期天为星期的开始
%w 星期(0-6),星期天为星期的开始
%W 一年中的星期数(00-53)星期一为星期的开始
%x 本地相应的日期表示
%X 本地相应的时间表示
%Z 当前时区的名称
%% %号本身
3、datetime、date模块
datetime.date
Attributes: year, month, and day. datetime.time
Attributes: hour, minute, second, microsecond, and tzinfo. datetime.datetime
Attributes: year, month, day, hour, minute, second, microsecond, and tzinfo. datetime.date.today() #datetime.date(2018, 4, 26) 返回不精确的时间
datetime.date.strftime(datetime.date.today(),'%Y%m%d %H%M%S') #时间解析'20180426 000000'
datetime.datetime.today() #datetime.datetime(2018, 4, 26, 20, 29, 25, 286001) 返回精确的时间
datetime.date.strftime(datetime.datetime.today(),'%Y%m%d %H%M%S') #时间解析'20180426 203144'
#间隔时间
res = datetime.datetime.today()+datetime.timedelta(days=1,minutes=5,seconds=5,weeks=5)
print(res.strftime('%Y-%m-%d'))
4、time
print(time.time()) #获取当前时间戳
# time.sleep(10)
today = time.strftime('%Y-%m-%d %H:%M:%S')
print(today) print(time.gmtime()) #默认取的是标准时区的时间
s=time.localtime(1514198608) #取到的是当前时区的时间,可以传入seconds
print(time.strftime('%Y-%m-%d %H:%M:%S',s))
time.localtime()#获取当前时间戳 结果:
1525507887.303
2018-05-05 16:11:27
time.struct_time(tm_year=2018, tm_mon=5, tm_mday=5, tm_hour=8, tm_min=11, tm_sec=27, tm_wday=5, tm_yday=125, tm_isdst=0)
2017-12-25 18:43:28
转换时间戳
time.strptime('2018-4-21','%Y-%m-%d')
time.struct_time(tm_year=2018, tm_mon=4, tm_mday=21, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=5, tm_yday=111, tm_isdst=-1) time.mktime(time.strptime('2018-4-21','%Y-%m-%d'))
1524240000.0
python time与datetime.date/datetime模块的更多相关文章
- Python之日期与时间处理模块(date和datetime)
本节内容 前言 相关术语的解释 时间的表现形式 time模块 datetime模块 时间格式码 总结 前言 在开发工作中,我们经常需要用到日期与时间,如: 作为日志信息的内容输出 计算某个功能的执行时 ...
- 【转】Python之日期与时间处理模块(date和datetime)
[转]Python之日期与时间处理模块(date和datetime) 本节内容 前言 相关术语的解释 时间的表现形式 time模块 datetime模块 时间格式码 总结 前言 在开发工作中,我们经常 ...
- 孤荷凌寒自学python第二十七天python的datetime模块及初识datetime.date模块
孤荷凌寒自学python第二十七天python的datetime模块及初识datetime.date模块 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.datetime模块 dateti ...
- Python第十五天 datetime模块 time模块 thread模块 threading模块 Queue队列模块 multiprocessing模块 paramiko模块 fabric模块
Python第十五天 datetime模块 time模块 thread模块 threading模块 Queue队列模块 multiprocessing模块 paramiko模块 fab ...
- time,datetime,calendar模块
Python中,与时间有关的模块有time,datetime和calendar. 1.时钟时间:time 在Python中,用三种方式来表示时间:时间戳,格式化时间字符串和结构化时间. 1)时间戳,就 ...
- 孤荷凌寒自学python第二十八天python的datetime.date模块
孤荷凌寒自学python第二十八天python的datetime.date模块 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.toordinal() 此方法将访问从公元1年1月1日至当 ...
- Python学习总结15:时间模块datetime & time & calendar (二)
二 .datetime模块 1. datetime中常量 1)datetime.MINYEAR,表示datetime所能表示的最小年份,MINYEAR = 1. 2)datetime.MAXYEAR ...
- python datetime date time详解
之前一直被datetime,date,time弄的有点乱,可能是因为看文档每太看明白,找到了两篇文章供大家阅读都是转载的,其中有些名词这里解释一下: 世界协调时间(Universal Time Coo ...
- Python全栈之路----常用模块----datetime模块详解
相比于time模块,datetime模块的接口则更直观,更容易调用. datetime模块定义了下面这几个类: datetime.date:表示日期的类,常用的属性有year,month,day: d ...
随机推荐
- Android基础新手教程——4.1.3 Activity登堂入室
Android基础新手教程--4.1.3 Activity登堂入室 标签(空格分隔): Android基础新手教程 本节引言: 好的,在学习了两节的Activity后相信大家已经知道怎样去使用Acti ...
- [Functional Programming] Combine Multiple State ADT Instances with the Same Input (converge(liftA2(constant)))
When combining multiple State ADT instances that depend on the same input, using chain can become qu ...
- Windows如何定时关机
方法一:首先在"开始"菜单点击"运行",输入"at xx:xx shoutdown -s" 可以实现定时关机,xx:xx指的是具体关机时间. ...
- Property with 'retain (or strong)' attribute must be of object type
AFNetworking 2.0 当Deployment Target 低于6.0时,AFURLConnectionOperation.h,AFURLSessionManager.h @propert ...
- cookie 与 session 的差别、联系
1.存放位置: Session 存放在server端. Cookie 存放在client: 2.保存形式: Session保存在server的内存中(在server端设置超时时间,与浏览器设置无关): ...
- 监听器如何获取Spring配置文件
我们在做项目的时候,会用到监听器去获取spring的配置文件,然后从中拿出我们需要的bean出来,比如做网站首页,假设商品的后台业务逻辑都做好了,我们需要创建一个监听器,在项目启动时将首页的数据查询出 ...
- javascript学习笔记(三)
1.与命名空间相关的方法以及属性 2.任何支持style特性的HTML元素在Javascript中都有一个对应的style属性.这个属性是CSSStyleDecalration的实例, 包含着通过HT ...
- nginx中ngx_list的数据结构
今天没事了,在查看nginx源代码中看到ngx_list的结构,发现设计为链表数组的形式,不知道为什么这样设计 struct ngx_list_part_s { void *elts;//指向数组的起 ...
- Atitit.数据索引 的种类以及原理实现机制 索引常用的存储结构
Atitit.数据索引 的种类以及原理实现机制 索引常用的存储结构 1. 索引的分类1 1.1. 索引的类型 按查找方式分,两种,分块索引 vs编号索引1 1.2. 按索引与数据的查找顺序可分为 正 ...
- 【转载】Js获取当前日期时间及其它操作
var myDate = new Date();myDate.getYear(); //获取当前年份(2位)myDate.getFullYear(); //获取完整的年份(4位,1 ...