-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdate.js
More file actions
151 lines (133 loc) · 3.88 KB
/
Copy pathdate.js
File metadata and controls
151 lines (133 loc) · 3.88 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* @file date.js
* @description Calendar helpers on the native Date prototype (side-effect module).
* Used by date-display.js and calendar.js for week/month navigation and YYYY_MM_DD keys.
* @author Thomas Lane
* @version $Revision: 453 $
* $Date: 2012-01-17 15:38:39 -0800 (Tue, 17 Jan 2012) $
* @modified G.E. Eidsness
* @version $Revisions: 001 $
* $Date: 2023-11-17 00:46:39 -0700 (Fri, 17 Nov 2023) $
* @version $Rewrite: 001 $
* $Date: 2026-07-28 11:36:29 -0700 (Tue, 28 July 2026) $
*/
// Separator for delimited date strings (matches jsonShows/ file naming)
Date.DELIM = "_";
// Week bounds (Sunday-start calendar)
Date.WEEKSTART = 0;
Date.WEEKEND = 6;
/**
* @returns {string} Day of week as an English word
*/
Date.prototype.getDayWord = function () {
return this.toLocaleDateString("en-US", { weekday: "long" });
};
/**
* @returns {string} Month of year as an English word
*/
Date.prototype.getMonthWord = function () {
// Use local month (not UTC) so names match getMonth()/getDelimDate()
return this.toLocaleDateString("en-US", { month: "long" });
};
/**
* @returns {Date} First day of this date's month
*/
Date.prototype.getMonthStart = function () {
return new Date(this.getFullYear(), this.getMonth(), 1);
};
/**
* @returns {Date} Last day of this date's month
*/
Date.prototype.getMonthEnd = function () {
return new Date(this.getFullYear(), this.getMonth() + 1, 0);
};
/**
* @returns {Date} First day of this date's week (Sunday)
*/
Date.prototype.getWeekStart = function () {
return new Date(
this.getFullYear(),
this.getMonth(),
this.getDate() - (this.getDay() - Date.WEEKSTART)
);
};
/**
* @returns {Date} Last day of this date's week (Saturday)
*/
Date.prototype.getWeekEnd = function () {
return new Date(
this.getFullYear(),
this.getMonth(),
this.getDate() + (Date.WEEKEND - this.getDay())
);
};
/**
* Mutates this date forward by one day.
*/
Date.prototype.incrementByDay = function () {
this.setDate(this.getDate() + 1);
};
/**
* Mutates this date backward by one day.
*/
Date.prototype.decrementByDay = function () {
this.setDate(this.getDate() - 1);
};
/**
* Mutates this date forward by seven days.
*/
Date.prototype.incrementByWeek = function () {
this.setDate(this.getDate() + 7);
};
/**
* Mutates this date backward by seven days.
*/
Date.prototype.decrementByWeek = function () {
this.setDate(this.getDate() - 7);
};
/**
* Mutates this date forward by one month, clamping the day if needed
* (e.g. Jan 31 → Feb 28/29 instead of skipping to March).
*/
Date.prototype.incrementByMonth = function () {
const firstNextMonth = new Date(this.getFullYear(), this.getMonth() + 1, 1);
const sameDayNextMonth = new Date(
this.getFullYear(),
this.getMonth() + 1,
this.getDate()
);
if (sameDayNextMonth.getMonth() !== firstNextMonth.getMonth()) {
this.setDate(firstNextMonth.getMonthEnd().getDate());
this.setMonth(firstNextMonth.getMonth());
} else {
this.setMonth(this.getMonth() + 1);
}
};
/**
* Mutates this date backward by one month, clamping the day if needed
* (e.g. Mar 31 → Feb 28/29 instead of landing on Mar 2/3).
*/
Date.prototype.decrementByMonth = function () {
const lastPrevMonth = this.getMonthStart();
lastPrevMonth.decrementByDay();
const sameDayPrevMonth = new Date(
this.getFullYear(),
this.getMonth() - 1,
this.getDate()
);
if (sameDayPrevMonth.getTime() > lastPrevMonth.getTime()) {
this.setDate(lastPrevMonth.getDate());
this.setMonth(lastPrevMonth.getMonth());
} else {
this.setMonth(this.getMonth() - 1);
}
};
/**
* @returns {string} Date as YYYY_MM_DD (zero-padded month and day)
*/
Date.prototype.getDelimDate = function () {
const year = this.getFullYear();
const month = String(this.getMonth() + 1).padStart(2, "0");
const day = String(this.getDate()).padStart(2, "0");
return year + Date.DELIM + month + Date.DELIM + day;
};