DEV Community

Discussion on: Custom util to get weekday name or number

Collapse
 
stegriff profile image
Ste Griffiths

Thanks for the post. I think you could improve it by removing the hardcoded English day names and using the JS built-in features to get the day of the week in your locale.

Typescript:

getWeekDays(locale): string[] {
    var baseDate = new Date(Date.UTC(2017, 0, 1)); // just a Sunday
    var weekDays = [];
    for (let i = 0; i < 7; i++) {
      let dayName = baseDate.toLocaleDateString(locale, { weekday: 'long' });
      dayName = dayName[0].toUpperCase() + dayName.slice(1);
      weekDays.push(dayName);
      baseDate.setDate(baseDate.getDate() + 1);
    }
    return weekDays;
  }
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ml318097 profile image
Mehul Lakhanpal

Great i did not know that. Thanks 😀