python对日期时间的操作

上传人:小** 文档编号:93481500 上传时间:2019-07-22 格式:DOC 页数:16 大小:31.40KB
返回 下载 相关 举报
python对日期时间的操作_第1页
第1页 / 共16页
python对日期时间的操作_第2页
第2页 / 共16页
python对日期时间的操作_第3页
第3页 / 共16页
python对日期时间的操作_第4页
第4页 / 共16页
python对日期时间的操作_第5页
第5页 / 共16页
点击查看更多>>
资源描述

《python对日期时间的操作》由会员分享,可在线阅读,更多相关《python对日期时间的操作(16页珍藏版)》请在金锄头文库上搜索。

1、Python对日期时间的操作简介:Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime。time模块我在之前的文章已经有所介绍,它提供 的接口与C标准库time.h基本一致。相比于time模块,datetime模块的接口则更直观、更容易调用。今天就来讲讲datetime模块。下面详细介绍这些类的使用方式。1 date类 date类表示一个日期。日期由年、月、日组成的。date类的构造函数如下: class datetime.date(year, month, day):参数的意义就不多作解释了,只是有几点要注意一下: year的范围是MINYEAR,

2、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.fromo

3、rdinal(ordinal):将Gregorian日历时间转换为date对象;(Gregorian Calendar:一种日历表示方法,类似于我国的农历,西方国家使用比较多,此处不详细展开讨论。) 使用例子:fromdatetimeimport*importtimeprintdate.max:,date.maxprintdate.min:,date.minprintdate.today():,date.today()printdate.fromtimestamp():,date.fromtimestamp(time.time()#-结果-#date.max:9999-12-31#date.m

4、in:0001-01-01#date.today():2010-04-06#date.fromtimestamp():2010-04-06fromdatetimeimport*importtimeprintdate.max:,date.maxprintdate.min:,date.minprintdate.today():,date.today()printdate.fromtimestamp():,date.fromtimestamp(time.time()#-结果-#date.max:9999-12-31#date.min:0001-01-01#date.today():2010-04-0

5、6#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,

6、以此类推; 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)printnow:,now,tomorrow:,tomorrowprinttimetuple():,now.timetuple(

7、)printweekday():,now.weekday()printisoweekday():,now.isoweekday()printisocalendar():,now.isocalendar()printisoformat():,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-06now=date(201

8、0,04,06)tomorrow=now.replace(day=07)printnow:,now,tomorrow:,tomorrowprinttimetuple():,now.timetuple()printweekday():,now.weekday()printisoweekday():,now.isoweekday()printisocalendar():,now.isocalendar()printisoformat():,now.isoformat()#-结果-#now:2010-04-06,tomorrow:2010-04-07#timetuple():(2010,4,6,0,

9、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 now#-结果

10、-#now:2010-04-06tomorrow:2010-04-07#timedelta:1day,0:00:00#2010-04-07#Truenow=date.today()tomorrow=now.replace(day=7)delta=tomorrow-nowprintnow:,now,tomorrow:,tomorrowprinttimedelta:,deltaprintnow+deltaprinttomorrownow#-结果-#now:2010-04-06tomorrow:2010-04-07#timedelta:1day,0:00:00#2010-04-07#True2 Ti

11、me类 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

12、= 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):返回自定义格式化字符串。在下面详细介绍;使用例子:fromdatetimeimport*tm=time(23,46,10)printtm:,tmprinthour:%d,minute:%d,second:%d,microsecond:%d%(tm.hour,tm.m

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 管理学资料

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号