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 :一种日历表示方法,类似于我国的农历,西方国家使用比较多,此处不详细展开讨论。)

    使用例子:

  1. from  datetime  import  *  
  2. import  time  
  3.   
  4. print   'date.max:' , date.max  
  5. print   'date.min:' , date.min  
  6. print   'date.today():' , date.today()  
  7. print   'date.fromtimestamp():' , date.fromtimestamp(time.time())  
  8.   
  9. # # ---- 结果 ----   
  10. # date.max: 9999-12-31   
  11. # date.min: 0001-01-01   
  12. # date.today(): 2010-04-06   
  13. # date.fromtimestamp(): 2010-04-06   
  1. from datetime import *  
  2. import time  
  3.   
  4. print 'date.max:', date.max  
  5. print 'date.min:', date.min  
  6. print 'date.today():', date.today()  
  7. print 'date.fromtimestamp():', date.fromtimestamp(time.time())  
  8.   
  9. # # ---- 结果 ----  
  10. # date.max: 9999-12-31  
  11. # date.min: 0001-01-01  
  12. # date.today(): 2010-04-06  
  13. # 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):自定义格式化字符串。在下面详细讲解。

    使用例子:

  1. now = date( 2010 ,  04 ,  06 )  
  2. tomorrow = now.replace(day = 07 )  
  3. print   'now:' , now,  ', tomorrow:' , tomorrow  
  4. print   'timetuple():' , now.timetuple()  
  5. print   'weekday():' , now.weekday()  
  6. print   'isoweekday():' , now.isoweekday()  
  7. print   'isocalendar():' , now.isocalendar()  
  8. print   'isoformat():' , now.isoformat()  
  9.   
  10. # # ---- 结果 ----   
  11. # now: 2010-04-06 , tomorrow: 2010-04-07   
  12. # timetuple(): (2010, 4, 6, 0, 0, 0, 1, 96, -1)   
  13. # weekday(): 1   
  14. # isoweekday(): 2   
  15. # isocalendar(): (2010, 14, 2)   
  16. # isoformat(): 2010-04-06   
  1. now = date(20100406)  
  2. tomorrow = now.replace(day = 07)  
  3. print 'now:', now, ', tomorrow:', tomorrow  
  4. print 'timetuple():', now.timetuple()  
  5. print 'weekday():', now.weekday()  
  6. print 'isoweekday():', now.isoweekday()  
  7. print 'isocalendar():', now.isocalendar()  
  8. print 'isoformat():', now.isoformat()  
  9.   
  10. # # ---- 结果 ----  
  11. # now: 2010-04-06 , tomorrow: 2010-04-07  
  12. # timetuple(): (2010, 4, 6, 0, 0, 0, 1, 96, -1)  
  13. # weekday(): 1  
  14. # isoweekday(): 2  
  15. # isocalendar(): (2010, 14, 2)  
  16. # isoformat(): 2010-04-06  

    date还对某些操作进行了重载,它允许我们对日期进行如下一些操作:

  • date2 = date1 + timedelta  # 日期加上一个间隔,返回一个新的日期对象(timedelta将在下面介绍,表示时间间隔)
  • date2 = date1 - timedelta   # 日期隔去间隔,返回一个新的日期对象
  • timedelta = date1 - date2   # 两个日期相减,返回一个时间间隔对象
  • date1 < date2  # 两个日期进行比较

    注: 对日期进行操作时,要防止日期超出它所能表示的范围。

    使用例子:

  1. now = date.today()  
  2. tomorrow = now.replace(day = 7 )  
  3. delta = tomorrow - now  
  4. print   'now:' , now,  ' tomorrow:' , tomorrow  
  5. print   'timedelta:' , delta  
  6. print  now + delta  
  7. print  tomorrow > now  
  8.   
  9. # # ---- 结果 ----   
  10. # now: 2010-04-06  tomorrow: 2010-04-07   
  11. # timedelta: 1 day, 0:00:00   
  12. # 2010-04-07   
  13. # True   
  1. now = date.today()  
  2. tomorrow = now.replace(day = 7)  
  3. delta = tomorrow - now  
  4. print 'now:', now, ' tomorrow:', tomorrow  
  5. print 'timedelta:', delta  
  6. print now + delta  
  7. print tomorrow > now  
  8.   
  9. # # ---- 结果 ----  
  10. # now: 2010-04-06  tomorrow: 2010-04-07  
  11. # timedelta: 1 day, 0:00:00  
  12. # 2010-04-07  
  13. # True  
0 0
原创粉丝点击