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
|
.pragma library
function diffMonths(dateA, dateB) {
var months;
months = (dateB.getFullYear() - dateA.getFullYear()) * 12;
months -= dateA.getMonth();
months += dateB.getMonth();
return Math.max(months, 0);
}
Date.leapYear = function(year) {
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
}
Date.daysInMonth = function(year, month) {
return [
31/*Jan*/, 28/*Feb*/, 31/*Mar*/, 30/*Apr*/, 31/*May*/, 30/*Jun*/,
31/*Jul*/, 31/*Aug*/, 30/*Sep*/, 31/*Oct*/, 30/*Nov*/, 31/*Dec*/
][month] + (month == 1) * Date.leapYear(year)
}
Date.prototype.midnight = function() {
var date = new Date(this)
date.setHours(0,0,0,0);
return date
}
Date.prototype.addDays = function(days) {
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
}
Date.prototype.addMonths = function(months) {
var date = new Date(this)
date.setMonth(date.getMonth() + months)
return date
}
|