python时间处理之date

  1. # -*- coding: utf-8 -*-                                   
  2. from datetime import * 
  3. import time  
  4. # 1. date常用的类方法和类属  

  5.  
  6. #date对象所能表示的最大日期: 9999-12-31  
  7. print 'date.max',date.max  
  8. #date对象所能表示的最小日期: 0001-01-01  
  9. print 'date.min',date.min  
  10. #返回一个表示当前本地日期的date对象: 2012-09-12  
  11. print 'date.today():', date.today()    
  12. #将Gregorian日历时间转换为date对象(Gregorian Calendar :一种日历表示方法,类似于我国的农历,西方国家使用比较多):   
  13. #1347442385.972转换为2012-09-12  
  14. print 'date.fromtimestamp():', date.fromtimestamp(time.time())   
  15.   
  16. # 2. date提供的实例方法和属性  

  17.   
  18. #获得年 月 日  
  19. now = date(2012,9,17)  
  20. print 'now.year:',now.year  
  21. print 'now.month:',now.month  
  22. print 'now.day:',now.day  
  23. #date.replace(year, month, day):生成一个新的日期对象  
  24. #用参数指定的年,月,日代替原有对象中的属性。(原有对象仍保持不变)  
  25. tomorrow = now.replace(day = 18)  
  26. nextmonth = now.replace(month = 10)  
  27. nextyear = now.replace(year = 2013)  
  28. print "tomorrow:",tomorrow," nextyear:",nextyear," nextmonth:",nextmonth  
  29. # 返回日期对应的time.struct_time对象;  
  30. # print: time.struct_time(tm_year=2012, tm_mon=9, tm_mday=17, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=261, tm_isdst=-1)  
  31. print "date.timetuple(): ",now.timetuple()   
  32. # 返回日期对应的Gregorian Calendar日期;  
  33. #print: 734763  
  34. print "date.toordinal(): ",str(now.toordinal())  
  35. #返回weekday,如果是星期一,返回0;如果是星期2,返回1,以此类推;   
  36. #print:0  
  37. print "date.weekday(): ",now.weekday()  
  38. #返回weekday,如果是星期一,返回1;如果是星期2,返回2,以此类推;  
  39. #print: 1  
  40. print 'date.isoweekday(): ',now.isoweekday()   
  41. #返回格式如(year,month,day)的元组;  
  42. #print: (2012, 38, 1)  
  43. print 'date.isocalendar():',now.isocalendar()   
  44. #返回格式如'YYYY-MM-DD’的字符串;  
  45. #print: '2012-09-17'  
  46. print 'date.isoformat():',now.isoformat()  
  47. #date.strftime(fmt):自定义格式化字符串。在下面详细讲解。  
  48.   
  49. #3. 日期操作   

  50.   
  51. #当前日期为2012年9月12日  
  52. now = date.today()  
  53. tomorrow = now.replace(day = 13)  
  54. delta = tomorrow - now  
  55. print "now: ",now,"tomorrow: ",tomorrow  
  56. #计算出间隔时间  
  57. #print: timedelta:  1 day, 0:00:00  
  58. print "timedelta: ",delta  
  59. #print: now + delta = tomorrow : 2012-09-13  
  60. print "now + delta = tomorrow :",now + delta  
  61. #print: tomorrow > now : True  
  62. print "tomorrow > now :",tomorrow > now  
  63. #print: tomorrow < now : False  
  64. print "tomorrow < now :",tomorrow < now 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值