js 日期操作扩展

内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Date.isLeapYear = function (year) {
return (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0));
};

Date.getDaysInMonth = function (year, month) {
return [31, (Date.isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
};

Date.prototype.isLeapYear = function () {
return Date.isLeapYear(this.getFullYear());
};

Date.prototype.getDaysInMonth = function () {
return Date.getDaysInMonth(this.getFullYear(), this.getMonth());
};

Date.prototype.addMonths = function (value) {
var n = this.getDate();
this.setDate(1);
this.setMonth(this.getMonth() + value);
this.setDate(Math.min(n, this.getDaysInMonth()));
return this;
};
Date.prototype.addDays = function(days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
}
//时间格式化 年月日时分秒
function dateymdhis(value){
var date = new Date(value);
var y = date.getFullYear();
var m = date.getMonth() + 1; m=(m+'').length==2? m:'0'+m;
var d = date.getDate(); d=(d+'').length==2? d:'0'+d;
var h=date.getHours(); h=(h+'').length==2? h:'0'+h;
var min=date.getMinutes(); min=(min+'').length==2? min:'0'+min;
var s=date.getSeconds(); s=(s+'').length==2? s:'0'+s;
return y + '-' +m + '-' + d + ' '+ h + ':' +min + ':' + s;
}