-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.js
72 lines (61 loc) · 1.89 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
const dateFieldFormatter = new Intl.DateTimeFormat('default', {
year: 'numeric',
month: 'short',
day: 'numeric',
});
const formattedDateField = (dt) =>
dt && dt instanceof Date ? dateFieldFormatter.format(dt) : '';
const monthYearFormatter = new Intl.DateTimeFormat('default', {
year: 'numeric',
month: 'long',
});
const formattedMonthYear = (dt) =>
dt && dt instanceof Date ? monthYearFormatter.format(dt) : '';
const firstDayOfMonth = (d) => new Date(d.yr, d.mo).getDay();
const lastDateInMonth = (d) => new Date(d.yr, d.mo + 1, 0).getDate();
const extractDateParts = (d) => ({
yr: d.getFullYear(),
mo: d.getMonth(),
dt: d.getDate(),
});
const isSelectableDate = (cur, d, validFrom) => {
let isValid = true;
const selectedDt = new Date(cur.yr, cur.mo, d).setHours(0, 0, 0, 0);
if (validFrom && validFrom instanceof Date)
isValid = selectedDt >= new Date(validFrom.getTime()).setHours(0, 0, 0, 0);
return isValid && selectedDt <= new Date().setHours(0, 0, 0, 0);
};
const selectedDate = (index, cur, dt) => {
if (!dt || !(dt instanceof Date)) return false;
return (
dt.getFullYear() === cur.yr &&
dt.getMonth() === cur.mo &&
dt.getDate() === index + 1
);
};
const daysOfWeek = () => {
const now = Date.now();
const millisecondsInDay = 24 * 60 * 60 * 1000;
const startDayInMs = now - new Date().getDay() * millisecondsInDay;
const formats = ['long', 'short'];
const formatters = formats.map(
(fmt) => new Intl.DateTimeFormat('default', { weekday: fmt })
);
return Array.from({ length: 7 }).map((_, i) => {
const d = new Date(startDayInMs + i * millisecondsInDay);
return formats.reduce(
(acc, fmt, idx) => ({ ...acc, [fmt]: formatters[idx].format(d) }),
{}
);
});
};
export {
formattedDateField,
formattedMonthYear,
firstDayOfMonth,
lastDateInMonth,
extractDateParts,
selectedDate,
daysOfWeek,
isSelectableDate,
};