-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdate.utils.js
More file actions
49 lines (46 loc) · 1.28 KB
/
Copy pathdate.utils.js
File metadata and controls
49 lines (46 loc) · 1.28 KB
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
40
41
42
43
44
45
46
47
48
49
// clear time portion of date object
if (typeof Date.prototype.clearTime != 'function') {
// see below for better implementation!
Date.prototype.clearTime = function (){
this.setHours(0);
this.setMinutes(0);
this.setSeconds(0);
this.setMilliseconds(0);
return this;
};
}
// Date formatting functions
// M/d/yyyy
if (typeof Date.prototype.format != 'function') {
Date.prototype.format = function () {
return (
(this.getMonth() + 1) + "/" +
(this.getDate()) + "/" +
(this.getFullYear())
);
};
}
// difference between dates in days
if (typeof Date.prototype.clearTime != 'differenceInDays') {
// see below for better implementation!
Date.prototype.differenceInDays = function (secondDate){
if (!secondDate instanceof Date) {
return null;
}
var timeDiff = this.getTime() - secondDate.getTime(); // in milliseconds
var dayDiff = Math.round(timeDiff / (1000 * 3600 * 24)); // divide by millis in day
return dayDiff;
};
}
var DateUtils = {
// determine the number of days in a given month in a given year
// @param month month of year from 1 to 12
// @param year four digit year
daysInMonth: function(month, year) {
if (month instanceof Date) {
year = month.getFullYear();
month = month.getMonth() + 1;
}
return new Date(year, month, 0).getDate();
},
}