-
Notifications
You must be signed in to change notification settings - Fork 0
/
date.js
137 lines (121 loc) · 3.88 KB
/
date.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
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
/**
* @projectDescription Extends the native Date class to perform sundry calendar specific operations
* @author Thomas Lane
* @version $Revision: 453 $
* $Date: 2012-01-17 15:38:39 -0800 (Tue, 17 Jan 2012) $
* @modified G.E. Eidsness
* @version $Revision: 015 $
* $Date: 2023-11-17 00:46:39 -0700 (Fri, 17 Nov 2023) $
*/
//Delimiter Constant used as separator
const DELIM = "_";
//Calendar Related Enumerations
const DAYSOFWEEK = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",];
const MONTHSOFYEAR = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December",];
const THEFIRST = 1;
const DAYSINWEEK = 6;
/**
* Returns the day value of the object as an english word
* @return day of week as a word
*/
Date.prototype.getDayWord = function() {
return DAYSOFWEEK[this.getDay()];
};
/**
* Returns the month value of the object as an english word
* @return month of year as a word
*/
Date.prototype.getMonthWord = function() {
return MONTHSOFYEAR[this.getUTCMonth()];
};
/**
* Returns a date object corresponding to the first day of the dates month
* @return date
*/
Date.prototype.getMonthStart = function() {
return new Date(this.getFullYear(), this.getMonth(), THEFIRST);
};
/**
* Returns a date object corresponding to the last day of the dates month
* @return date
*/
Date.prototype.getMonthEnd = function() {
return new Date(this.getFullYear(), this.getMonth() + 1, 0);
};
/**
* Returns a date object corresponding to the first day of the dates week
* @return date
*/
Date.prototype.getWeekStart = function() {
return new Date(
this.getFullYear(),
this.getMonth(),
this.getDate() - this.getDay()
);
};
/**
* Returns a date object corresponding to the last day of the dates week
* @return date
*/
Date.prototype.getWeekEnd = function() {
return new Date(
this.getFullYear(),
this.getMonth(),
this.getDate() + (DAYSINWEEK - this.getDay())
);
};
/**
* Increments the value of the date object as by a single day
* post: the date will have been increased by a single day
*/
Date.prototype.incrementByDay = function() {
this.setDate(this.getDate() + 1);
};
/**
* Decrements the value of the date object as by a single day
* post: the date will have been descreased by a single day
*/
Date.prototype.decrementByDay = function() {
this.setDate(this.getDate() - 1);
};
/**
* Increments the value of the date object by seven days
* post: the date will have been increased by seven days
*/
Date.prototype.incrementByWeek = function() {
this.setDate(this.getDate() + 7);
};
/**
* Decrements the value of the date object by seven days
* post: the date will have been decreased by seven days
*/
Date.prototype.decrementByWeek = function() {
this.setDate(this.getDate() - 7);
};
/** fixed 2024-08-17 00:46:39 -0700 (Fri, 17 Nov 2023)
* Increments the value of the date object by a month
* Only results in a single month increase
* post: the date will have been increased by a single month
*/
Date.prototype.incrementByMonth = function() {
this.setMonth(this.getMonth() + 1);
};
/**
* Decrements the value of the date object by a month
* Always results in a single month decrease
* post: the date will have been increased by a single month
*/
Date.prototype.decrementByMonth = function() {
this.setMonth(this.getMonth() - 1);
};
/** !! Important !!
* Returns value of the date object in the form YYYY_MM_DD
* with the possibility of single digit months and days
* @return string
*/
Date.prototype.getDelimDate = function() {
let day = this.getDate().toString().padStart(2, '0');
let month = (this.getMonth() + 1).toString().padStart(2, '0');
let year = this.getFullYear();
return `${year}${DELIM}${month}${DELIM}${day}`;
};