-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.js
77 lines (67 loc) · 2.16 KB
/
utils.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
function dateFromISODateString(str) {
return new Date(str.replaceAll("-", "/"))
}
function toISODateString(date) {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0'); // Months are zero-based, so add 1
const day = String(date.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
}
function toShortDateString(date) {
let dateParts = date.toDateString().split(' ')
dateParts.shift()
return dateParts.join('-')
}
function stripFileExtension(str) {
return str.split(".")[0]
}
function shortDate(date) {
let dateParts = date.toDateString().split(' ')
dateParts.shift()
return dateParts.join('-')
}
function jsonForDate(date) {
return toISODateString(date) + ".json"
}
/**
* @date The date to begin searching for Monday from.
*
* @forward boolean which determines whether if the date is Monday it
* returns that day, or the following Monday
*/
function findNextMonday(date, forward=false) {
const result = new Date(date);
const day = result.getDay();
if (day==1) {
let plus = 0;
if (forward !== false) {
plus = 7;
}
result.setDate(result.getDate() + plus);
return result
}
const diff = (day === 0 ? 1 : 8 - day); // If it's Sunday (day 0), add 1 day. Otherwise, add the difference to Monday.
result.setDate(result.getDate() + diff);
return result;
}
/**
* @date The date to begin searching for Monday from.
*/
function findPreviousMonday(date) {
const result = new Date(date);
const day = result.getDay();
const diff = (day === 0 ? 1 : 8 - day); // If it's Sunday (day 0), add 1 day. Otherwise, add the difference to Monday.
result.setDate(result.getDate() - diff);
return result;
}
function areSameDate(date1, date2) {
return date1.toDateString() == date2.toDateString();
}
exports.toISODateString = toISODateString
exports.toShortDateString = toShortDateString
exports.stripFileExtension = stripFileExtension
exports.jsonForDate = jsonForDate
exports.findNextMonday = findNextMonday
exports.dateFromISODateString = dateFromISODateString
exports.areSameDate = areSameDate
exports.findPreviousMonday = findPreviousMonday