python对日期时间的操作
Python对日期时间的操作简介:Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime。time模块我在之前的文章已经有所介绍,它提供 的接口与C标准库time.h基本一致。相比于time模块,datetime模块的接口则更直观、更容易调用。今天就来讲讲datetime模块。下面详细介绍这些类的使用方式。1 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 2 Time类 time类表示时间,由时、分、秒以及微秒组成。(我不是从火星来的)time类的构造函数如下: class datetime.time(hour , minute , second , microsecond , tzinfo ) :各参数的意义不作解释,这里留意一下参数tzinfo,它表示时区信息。注意一下各参数的取值范围:hour的范围为0, 24),minute的范围为0, 60),second的范围为0, 60),microsecond的范围为0, 1000000)。 time类定义的类属性:· time.min、time.max:time类所能表示的最小、最大时间。其中,time.min = time(0, 0, 0, 0), time.max = time(23, 59, 59, 999999);· time.resolution:时间的最小单位,这里是1微秒; time类提供的实例方法和属性:· time.hour、time.minute、time.second、time.microsecond:时、分、秒、微秒;· time.tzinfo:时区信息;· time.replace( hour , minute , second , microsecond , tzinfo ):创建一个新的时间对象,用参数指定的时、分、秒、微秒代替原有对象中的属性(原有对象仍保持不变);· time.isoformat():返回型如"HH:MM:SS"格式的字符串表示;· time.strftime(fmt):返回自定义格式化字符串。在下面详细介绍;使用例子:from datetime import * tm = time(23 , 46 , 10 ) print 'tm:' , tm print 'hour: %d, minute: %d, second: %d, microsecond: %d' % (tm.hour, tm.m