Date对象扩展

近期做了一个POC,需要在浏览器端做一些日期的处理工作,除去其中跟公司产品相关的部分,其它的代码还是比较通用的,主要有两个功能:

  1. 根据一个Date对象获取相对应的特殊时间点,比如一天、一个月或者一年的起止时间;
  2. 在一个Date对象上加减相应的时间值
js 代码
  1. /**  
  2.  * This file is a Date extension which can handle specified moments  
  3.  *   
  4. /**  
  5.  * set the Date object to a specific local moment  
  6.  * @param {enum} iMoment the specific moment, can reference those extened properties of Date.  
  7.  */  
  8. Date.prototype.setToLocalMoment=function(iMoment)   
  9. {   
  10.     switch (iMoment) {   
  11.         case Date.MOMENT_DATE_START:   
  12.             this.setHours(0,0,0,0);   
  13.             break;   
  14.         case Date.MOMENT_DATE_END:   
  15.             this.setHours(23,59,59,999);   
  16.             break;   
  17.         case Date.MOMENT_MONTH_START:   
  18.             this.setHours(0,0,0,0);   
  19.             this.setDate(1);   
  20.             break;   
  21.         case Date.MOMENT_MONTH_END:   
  22.             this.setHours(23,59,59,999);   
  23.             //can not get the last day of a month directly,   
  24.             //so set to the first day in next month, and minus one day.   
  25.             //numDate is start from 1, so 0 means the last day before 1st    
  26.             this.setMonth(this.getMonth()+1, 0);   
  27.             break;   
  28.         case Date.MOMENT_YEAR_START:   
  29.             this.setHours(0,0,0,0);   
  30.             //numMonth is start from 0, to 11, but numDate is from 1 to 31, weird    
  31.             this.setMonth(0, 1);   
  32.             break;   
  33.         case Date.MOMENT_YEAR_END:   
  34.             this.setHours(23,59,59,999);   
  35.             this.setMonth(11, 31);   
  36.             break;   
  37.         default:   
  38.     }   
  39. }   
  40.   
  41. /**  
  42.  * plus the specific values to this Date object  
  43.  * All parameters are optional, if one of them is positive, it's plused, otherwise, it's minused.   
  44.  * @param {int} iYear  
  45.  * @param {int} iMonth  
  46.  * @param {int} iDate  
  47.  * @param {int} iHours  
  48.  * @param {int} iMinutes  
  49.  * @param {int} iSeconds  
  50.  * @param {int} iMilliseconds  
  51.  */  
  52. Date.prototype.plus=function(iYear,iMonth,iDate,iHours,iMinutes,iSeconds,iMs)   
  53. {   
  54.     var ilYear=this.getFullYear(); ilYear+=iYear?iYear:0;   
  55.     var ilMonth=this.getMonth(); ilMonth+=iMonth?iMonth:0;   
  56.     var ilDate=this.getDate(); ilDate+=iDate?iDate:0;   
  57.     var ilHours=this.getHours(); ilHours+=iHours?iHours:0;   
  58.     var ilMinutes=this.getMinutes(); ilMinutes+=iMinutes?iMinutes:0;   
  59.     var ilSeconds=this.getSeconds(); ilSeconds+=iSeconds?iSeconds:0;   
  60.     var ilMs=this.getMilliseconds(); ilMs+=iMs?iMs:0;   
  61.     var date=new Date();   
  62.     date.setFullYear(ilYear, ilMonth, ilDate);   
  63.     date.setHours(ilHours,ilMinutes, ilSeconds,ilMs);   
  64.     return date;   
  65. }   
  66. Date.prototype.minus=function(iYear,iMonth,iDate,iHours,iMinutes,iSeconds,iMs)   
  67. {   
  68.     return this.plus(-iYear,-iMonth,-iDate,-iHours,-iMinutes,-iSeconds,-iMs);   
  69. }   
  70. /**  
  71.  * extened properties of Date: to define a set of special moment   
  72.  */  
  73. Date.MOMENT_DATE_START=100;   
  74. Date.MOMENT_DATE_END=101;   
  75. Date.MOMENT_MONTH_START=110;   
  76. Date.MOMENT_MONTH_END=111;   
  77. Date.MOMENT_YEAR_START=120;   
  78. Date.MOMENT_YEAR_END=121;   
  79.   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值