-
Notifications
You must be signed in to change notification settings - Fork 20
Description
I have been used this perfect tool a few days ago and noticed that there is a problem with the "genDayOfWeek" utils function.
In my local calendar, we are counting the week from Sunday to Saturday and unfortunately, when I use the function "genTimeBlock" that uses the "genDayOfWeek" function, it does not count from "Sunday" and the "Sunday" that in my local calendar is the beginning of the week, the function count "Sunday" like it's in the next week, and not as the beginning of the week.
The source of the problem is in this row:
return new Date(2019-07-${str2numberString[DayOfWeekString.toLowerCase()]}T00:00:00);
because it looks at a specific week in 2019, that is the 1st day of the month is on "Monday".
To solve it locally, I created my own function and changed the number of days to make it count the "Sunday" as the first day of the week.
My suggestion is to make this function more general by adding some "What is your first day of the week?"
Code of the functions:
const genDayOfWeek = (DayOfWeekString) => {
/*
DayOfWeekString : SUN, MON, TUE, WED, THU, FRI, SAT
type : string
*/
if (typeof DayOfWeekString !== 'string') {
throw new Error(`genDayOfWeek got parameter type: ${typeof DayOfWeekString}, but string expected`);
}
const str2numberString = {
'mon': '01',
'tue': '02',
'wed': '03',
'thu': '04',
'fri': '05',
'sat': '06',
'sun': '07',
'월': '01',
'화': '02',
'수': '03',
'목': '04',
'금': '05',
'토': '06',
};
return new Date(`2019-07-${str2numberString[DayOfWeekString.toLowerCase()]}T00:00:00`);
};
const genTimeBlock = (dayOfWeek, hours = 0, minutes = 0) => {
const date = genDayOfWeek(dayOfWeek);
date.setHours(hours);
if (minutes != null) {
date.setMinutes(minutes);
}
return date;
};