---
title: "Date对象扩展"
author: "songofhawk"
date: "2007-04-10T22:30:19Z"
tags: []
categories: ["Web前端"]
slug: "date"
draft: false
lastmod: "2007-04-10T22:30:19Z"
id: 69745
url: "https://songofhawk.iteye.com/blog/69745"
---

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

1. 根据一个Date对象获取相对应的特殊时间点，比如一天、一个月或者一年的起止时间；
2. 在一个Date对象上加减相应的时间值

js 代码

01. /\**
02. * This file is a Date extension which can handle specified moments
03. \*
04. /\**
05. * set the Date object to a specific local moment
06. * @param {enum} iMoment the specific moment, can reference those extened properties of Date.
07. \*/
08. Date.prototype.setToLocalMoment=function(iMoment)
09. {
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;