ITeye博客

Date对象扩展

songofhawk · 2007-04-10 22:30 · Javascript · Web前端 · Markdown

近期做了一个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. * plus the specific values to this Date object
  42. * All parameters are optional, if one of them is positive, it's plused, otherwise, it's minused.
  43. * @param {int} iYear
  44. * @param {int} iMonth
  45. * @param {int} iDate
  46. * @param {int} iHours
  47. * @param {int} iMinutes
  48. * @param {int} iSeconds
  49. * @param {int} iMilliseconds
  50. */
  51. Date.prototype.plus=function(iYear,iMonth,iDate,iHours,iMinutes,iSeconds,iMs)
  52. {
  53. var ilYear=this.getFullYear(); ilYear+=iYear?iYear:0;
  54. var ilMonth=this.getMonth(); ilMonth+=iMonth?iMonth:0;
  55. var ilDate=this.getDate(); ilDate+=iDate?iDate:0;
  56. var ilHours=this.getHours(); ilHours+=iHours?iHours:0;
  57. var ilMinutes=this.getMinutes(); ilMinutes+=iMinutes?iMinutes:0;
  58. var ilSeconds=this.getSeconds(); ilSeconds+=iSeconds?iSeconds:0;
  59. var ilMs=this.getMilliseconds(); ilMs+=iMs?iMs:0;
  60. var date=new Date();
  61. date.setFullYear(ilYear, ilMonth, ilDate);
  62. date.setHours(ilHours,ilMinutes, ilSeconds,ilMs);
  63. return date;
  64. }
  65. Date.prototype.minus=function(iYear,iMonth,iDate,iHours,iMinutes,iSeconds,iMs)
  66. {
  67. return this.plus(-iYear,-iMonth,-iDate,-iHours,-iMinutes,-iSeconds,-iMs);
  68. }
  69. /**
  70. * extened properties of Date: to define a set of special moment
  71. */
  72. Date.MOMENT_DATE_START=100;
  73. Date.MOMENT_DATE_END=101;
  74. Date.MOMENT_MONTH_START=110;
  75. Date.MOMENT_MONTH_END=111;
  76. Date.MOMENT_YEAR_START=120;
  77. Date.MOMENT_YEAR_END=121;

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。