Get the first day of the week with JavaScript
ellie.wtf·81w
Preview
Report Post

I’m currently building a calendar data display for Atuin, and wanted to ensure that the week started with the correct day.

While I’m a big fan of things being configurable and flexible, I don’t want to introduce a new config option unless it’s required!

Luckily browser localisation exposes plenty of options.

Get a Locale object

First we need a Locale! They’re created like so

new Intl.Locale('CODE')

// eg

new Intl.Locale('en-GB')

We can get the code as follows

new Intl.Locale(navigator.language)

This is a set by the user in their browser

Get the weekinfo

Once we have the Locale, it’s simple

let locale = new Intl.Locale(navigator.language);
let weekinfo = locale.getWeekInfo();
console.log(weekinfo...

Similar Posts

Loading similar posts...

Keyboard Shortcuts

Navigation
Next / previous item
j/k
Open post
oorEnter
Preview post
v
Post Actions
Love post
a
Like post
l
Dislike post
d
Undo reaction
u
Recommendations
Add interest / feed
Enter
Not interested
x
Go to
Home
gh
Interests
gi
Feeds
gf
Likes
gl
History
gy
Changelog
gc
Settings
gs
Browse
gb
Search
/
General
Show this help
?
Submit feedback
!
Close modal / unfocus
Esc

Press ? anytime to show this help