Menu

1 / 2

Stop Hardcoding Sunday as Day One. Intl.Locale Has getWeekInfo()

DEV Community: frontend·JKC·about 16 hours ago
#aO3r1UWH
#devtofrontend#devto#dev#getday#locale
Reading 0:00
15s threshold

TL;DR: new Intl.Locale("en-GB").getWeekInfo() tells you the week starts on Monday and the weekend is Saturday and Sunday. With Firefox 153, the whole Intl.Locale info family is Baseline Newly available, so you can delete that hand-rolled "first day of week" table.

The line nobody wants to touch

Every date picker has it somewhere.

const FIRST_DAY = 0 ; // Sunday. Probably. Ask product.

Enter fullscreen mode

Exit fullscreen mode

Then a user in London opens the calendar and the week starts on the wrong day. Someone adds if (locale === "en-GB") ... , then a lookup table, then a ticket about Israel, where the weekend is Friday and Saturday. The table grows. Nobody is sure where the data came from.

The browser already ships that data. It has been sitting in the Unicode CLDR tables that power Intl.DateTimeFormat all along.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.