-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDate.gs
45 lines (40 loc) · 1.24 KB
/
Date.gs
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
//
// Date.gs
// ============================================================================
// Custom methods for working with dates
//
Date.prototype.toEpoch = function() {
// Convert date to Epoch time, the number of seconds since January 1, 1970.
// Return {Number} Unix Epoch timestamp for this date
return this.getTime() / 1000;
}
Date.prototype.MMDDYYYY = function() {
// Export date in American date format
// Return {String} date in MM/DD/YYYY format
var month = this.getMonth() + 1;
var date = this.getDate();
var year = this.getFullYear();
return month + '/' + date + '/' + year;
}
Date.prototype.incDate = function(days) {
// Increment date by one day
// Return {Date, this} for chaining
this.setDate(this.getDate() + days);
return this;
}
Date.prototype.getWeekStart = function() {
// Get the first day of the week given an arbitrary day.
// Return {Date} A new date object
var d = new Date(this.getTime());
var monday = 1;
while (d.getDay() !== monday) {
d.setDate(d.getDate() - 1);
}
return d;
}
Date.prototype.minusYears = function(x) {
// Subtract x years from the current date
// Return {Date, this} this Date object instance, for chaining
this.setFullYear(this.getFullYear() - x);
return this;
}