Dates.js
796 Bytes
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
function toYMD(dateObj) {
const year = dateObj.getFullYear();
const month = dateObj.getMonth() + 1;
const date = dateObj.getDate();
return { year, month, date };
}
function toYMDStr(dateObj) {
return [
dateObj.getFullYear(),
dateObj.getMonth() + 1,
dateObj.getDate(),
].join("/");
}
function toSunday(dateObj) {
const day = dateObj.getDay();
moveDate(dateObj, "day", -day);
}
function moveDate(dateObj, scope, distance) {
switch (scope) {
case "month":
dateObj.setMonth(dateObj.getMonth() + distance);
break;
case "week":
dateObj.setDate(dateObj.getDate() + distance * 7);
break;
case "day":
dateObj.setDate(dateObj.getDate() + distance);
break;
default:
}
}
export { toYMD, toYMDStr, toSunday, moveDate };