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模块的更多相关文章

  1. Python之日期与时间处理模块(date和datetime)

    本节内容 前言 相关术语的解释 时间的表现形式 time模块 datetime模块 时间格式码 总结 前言 在开发工作中,我们经常需要用到日期与时间,如: 作为日志信息的内容输出 计算某个功能的执行时 ...

  2. 【转】Python之日期与时间处理模块(date和datetime)

    [转]Python之日期与时间处理模块(date和datetime) 本节内容 前言 相关术语的解释 时间的表现形式 time模块 datetime模块 时间格式码 总结 前言 在开发工作中,我们经常 ...

  3. 孤荷凌寒自学python第二十七天python的datetime模块及初识datetime.date模块

    孤荷凌寒自学python第二十七天python的datetime模块及初识datetime.date模块 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.datetime模块 dateti ...

  4. Python第十五天 datetime模块 time模块 thread模块 threading模块 Queue队列模块 multiprocessing模块 paramiko模块 fabric模块

    Python第十五天  datetime模块 time模块   thread模块  threading模块  Queue队列模块  multiprocessing模块  paramiko模块  fab ...

  5. time,datetime,calendar模块

    Python中,与时间有关的模块有time,datetime和calendar. 1.时钟时间:time 在Python中,用三种方式来表示时间:时间戳,格式化时间字符串和结构化时间. 1)时间戳,就 ...

  6. 孤荷凌寒自学python第二十八天python的datetime.date模块

     孤荷凌寒自学python第二十八天python的datetime.date模块 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.toordinal() 此方法将访问从公元1年1月1日至当 ...

  7. Python学习总结15:时间模块datetime & time & calendar (二)

    二 .datetime模块  1. datetime中常量 1)datetime.MINYEAR,表示datetime所能表示的最小年份,MINYEAR = 1. 2)datetime.MAXYEAR ...

  8. python datetime date time详解

    之前一直被datetime,date,time弄的有点乱,可能是因为看文档每太看明白,找到了两篇文章供大家阅读都是转载的,其中有些名词这里解释一下: 世界协调时间(Universal Time Coo ...

  9. Python全栈之路----常用模块----datetime模块详解

    相比于time模块,datetime模块的接口则更直观,更容易调用. datetime模块定义了下面这几个类: datetime.date:表示日期的类,常用的属性有year,month,day: d ...

随机推荐

  1. lodash camelCase 驼峰写法

    _.camelCase([string='']) 转换字符串为 驼峰写法 _.camelCase('Foo Bar'); // => 'fooBar' _.camelCase('--foo-ba ...

  2. 单用户模式下mount -o remount,rw / 有大用途

    我们的Linux系统在无法启动时候,通常需要进入单用户模式下进行修改一些配置文件,或调整一些参数方可.但是在进入单用户模式后,我们的/文件系统是只读模式,无法进行修改,那么这个时候我们就需要用到一条命 ...

  3. The Unix Tools Are Your Friends

    The Unix Tools Are Your Friends Diomidis Spinellis IF, ON MY WAY TO EXILE ON A DESERT ISLAND, I had ...

  4. MySQL 5.6数据导入报 GTID 相关错误

    从阿里云备份数据后还原到本地,用命令行 mysql -uroot -p --default-character-set=<character> -f <dbname> < ...

  5. 《DirectX 9.0 3D游戏开发编程基础》 第一章 初始化Direct3D 读书笔记

    REF设备 参考光栅设备,他能以软件计算方式完全支持Direct3D Api.借助Ref设备,可以在代码中使用那些不为当前硬件所支持的特性,并对这此特性进行测试. D3DDEVTYPE 在程序代码中, ...

  6. PHP多线程处理问题

    近日工作中涉及到项目同时处理多个线程问题时,在网上找到了PHP的pthreads扩展以及curl_multi_init函数,具体如下: 一 .windows下安装php真正的多线程扩展pthreads ...

  7. objective-c的观察者模式

    addObserver即添加消息响应函数.postNotificationName即发消息.

  8. Atitit.code base view 视图的实现原理

    Atitit.code base view 视图的实现原理 1. 视图的执行算法:1 2. 不可更新的视图:1 3. 关于视图的可插入性:insert2 4. 视图定义3 5. 调用3 1. 视图的执 ...

  9. C 调用 lua 函数

    C 调用 lua 函数 需要考虑的问题: 1. 使用 lua_pcall 可以调用 lua 函数,首先把 lua 函数入栈,然后把参数入栈, lua_pcall(luaState, 参数个数, 返回值 ...

  10. 小程序swiper配置参数使用

    不管什么项目,一个轮播是基本少不了的,现在就来踩下微信小程序的swiper吧! 首先打开文档,可以看到下面这些参数:(https://mp.weixin.qq.com/debug/wxadoc/dev ...