Python 之 datetime模块
来源:互联网 发布:预谋邂逅知乎 编辑:程序博客网 时间:2024/05/29 17:41
datetime模块定义了两个常量:datetime.MINYEAR和datetime.MAXYEAR,分别表示datetime所能表示的最 小、最大年份。其中,MINYEAR = 1,MAXYEAR = 9999。(对于偶等玩家,这个范围已经足够用矣~~)
datetime模块定义了下面这几个类:
- datetime.date:表示日期的类。常用的属性有year, month, day;
- datetime.time:表示时间的类。常用的属性有hour, minute, second, microsecond;
- datetime.datetime:表示日期时间。
- datetime.timedelta:表示时间间隔,即两个时间点之间的长度。
- datetime.tzinfo:与时区有关的相关信息。(这里不详细充分讨论该类,感兴趣的童鞋可以参考python手册)
注 :上面这些类型的对象都是不可变(immutable)的。
下面详细介绍这些类的使用方式。
date类
date类表示一个日期。日期由年、月、日组成(地球人都知道~~)。date类的构造函数如下:
class datetime.date(year, month, day):参数的意义就不多作解释了,只是有几点要注意一下:
- year的范围是[MINYEAR, MAXYEAR],即[1, 9999];
- month的范围是[1, 12]。(月份是从1开始的,不是从0开始的~_~);
- day的最大值根据给定的year, month参数来决定。例如闰年2月份有29天;
date类定义了一些常用的类方法与类属性,方便我们操作:
- date.max、date.min:date对象所能表示的最大、最小日期;
- date.resolution:date对象表示日期的最小单位。这里是天。
- date.today():返回一个表示当前本地日期的date对象;
- date.fromtimestamp(timestamp):根据给定的时间戮,返回一个date对象;
- datetime.fromordinal(ordinal):将Gregorian日历时间转换为date对象;(Gregorian Calendar :一种日历表示方法,类似于我国的农历,西方国家使用比较多,此处不详细展开讨论。)
使用例子:
- from datetime import *
- import time
- print 'date.max:' , date.max
- print 'date.min:' , date.min
- print 'date.today():' , date.today()
- print 'date.fromtimestamp():' , date.fromtimestamp(time.time())
- # # ---- 结果 ----
- # date.max: 9999-12-31
- # date.min: 0001-01-01
- # date.today(): 2010-04-06
- # date.fromtimestamp(): 2010-04-06
- from datetime import *
- import time
- print 'date.max:', date.max
- print 'date.min:', date.min
- print 'date.today():', date.today()
- print 'date.fromtimestamp():', date.fromtimestamp(time.time())
- # # ---- 结果 ----
- # date.max: 9999-12-31
- # date.min: 0001-01-01
- # date.today(): 2010-04-06
- # date.fromtimestamp(): 2010-04-06
date提供的实例方法和属性:
- date.year、date.month、date.day:年、月、日;
- date.replace(year, month, day):生成一个新的日期对象,用参数指定的年,月,日代替原有对象中的属性。(原有对象仍保持不变)
- date.timetuple():返回日期对应的time.struct_time对象;
- date.toordinal():返回日期对应的Gregorian Calendar日期;
- date.weekday():返回weekday,如果是星期一,返回0;如果是星期2,返回1,以此类推;
- data.isoweekday():返回weekday,如果是星期一,返回1;如果是星期2,返回2,以此类推;
- date.isocalendar():返回格式如(year,month,day)的元组;
- date.isoformat():返回格式如'YYYY-MM-DD’的字符串;
- date.strftime(fmt):自定义格式化字符串。在下面详细讲解。
使用例子:
- now = date( 2010 , 04 , 06 )
- tomorrow = now.replace(day = 07 )
- print 'now:' , now, ', tomorrow:' , tomorrow
- print 'timetuple():' , now.timetuple()
- print 'weekday():' , now.weekday()
- print 'isoweekday():' , now.isoweekday()
- print 'isocalendar():' , now.isocalendar()
- print 'isoformat():' , now.isoformat()
- # # ---- 结果 ----
- # now: 2010-04-06 , tomorrow: 2010-04-07
- # timetuple(): (2010, 4, 6, 0, 0, 0, 1, 96, -1)
- # weekday(): 1
- # isoweekday(): 2
- # isocalendar(): (2010, 14, 2)
- # isoformat(): 2010-04-06
- now = date(2010, 04, 06)
- tomorrow = now.replace(day = 07)
- print 'now:', now, ', tomorrow:', tomorrow
- print 'timetuple():', now.timetuple()
- print 'weekday():', now.weekday()
- print 'isoweekday():', now.isoweekday()
- print 'isocalendar():', now.isocalendar()
- print 'isoformat():', now.isoformat()
- # # ---- 结果 ----
- # now: 2010-04-06 , tomorrow: 2010-04-07
- # timetuple(): (2010, 4, 6, 0, 0, 0, 1, 96, -1)
- # weekday(): 1
- # isoweekday(): 2
- # isocalendar(): (2010, 14, 2)
- # isoformat(): 2010-04-06
date还对某些操作进行了重载,它允许我们对日期进行如下一些操作:
- date2 = date1 + timedelta # 日期加上一个间隔,返回一个新的日期对象(timedelta将在下面介绍,表示时间间隔)
- date2 = date1 - timedelta # 日期隔去间隔,返回一个新的日期对象
- timedelta = date1 - date2 # 两个日期相减,返回一个时间间隔对象
- date1 < date2 # 两个日期进行比较
注: 对日期进行操作时,要防止日期超出它所能表示的范围。
使用例子:
- now = date.today()
- tomorrow = now.replace(day = 7 )
- delta = tomorrow - now
- print 'now:' , now, ' tomorrow:' , tomorrow
- print 'timedelta:' , delta
- print now + delta
- print tomorrow > now
- # # ---- 结果 ----
- # now: 2010-04-06 tomorrow: 2010-04-07
- # timedelta: 1 day, 0:00:00
- # 2010-04-07
- # True
- now = date.today()
- tomorrow = now.replace(day = 7)
- delta = tomorrow - now
- print 'now:', now, ' tomorrow:', tomorrow
- print 'timedelta:', delta
- print now + delta
- print tomorrow > now
- # # ---- 结果 ----
- # now: 2010-04-06 tomorrow: 2010-04-07
- # timedelta: 1 day, 0:00:00
- # 2010-04-07
- # True
0 0
- Python datetime模块之datetime
- Python模块之----datetime
- Python 之 datetime模块
- Python datetime 模块之timedelta
- Python datetime模块之date
- Python datetime模块之time
- Python datetime 模块之timedelta
- Python模块学习之time datetime模块
- python之time和datetime模块
- python模块系列之 - time,datetime,calendar
- python---time,datetime模块
- Python模块学习 ---- datetime
- Python模块学习 ---- datetime
- Python模块学习 ---- datetime
- Python模块学习 ---- datetime
- python datetime模块 简介
- Python模块学习 ---- datetime
- Python模块学习 ---- datetime
- HDU1254 推箱子(BFS)
- 【Postgresql源码分析之一】主备同步复制介绍
- 剑指offer49--第一个不重复的字符
- MySQL外键约束OnDelete和OnUpdate的使用
- HDU 5725 Game(计数)
- Python 之 datetime模块
- php面向对象和面向过程
- Bestcoder_Dertouzos
- Java enum的用法详解
- C++-glog/gflags/protobuf
- JAVA中怎么保留小数点后两位
- 静态方法、实例方法和域
- HDU 1856
- 使用再生龙制作linux系统镜像及还原