-
Notifications
You must be signed in to change notification settings - Fork 35
/
isodate.js
24 lines (21 loc) · 865 Bytes
/
isodate.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
// date helper functions
module.exports = {
_today_date: getNdaysAgoDate(0),
get today() {
return this._today_date;
},
get yesterday() { return getNdaysAgoDate(1) },
get year_ago() { return getNdaysAgoDate(365) },
get two_years_ago() { return getNdaysAgoDate(365*2) },
diff: function(first, second) {
var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
var firstDate = new Date(first);
var secondDate = new Date(second);
return Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay)));
}
}
function getNdaysAgoDate(n) {
let tzoffset = (new Date()).getTimezoneOffset() * 60000; //offset in milliseconds
let localISOTime = (new Date(Date.now() - (n * 86400000) - tzoffset)).toISOString().slice(0, -1);
return localISOTime.split('T')[0]
}