I’d like to display in my i18n-ed app a list of the 7 weekdays:
JavaScript
x
2
1
Sunday, Monday, Tuesday Saturday
2
I rely on the Intl global object for formatting date/time but I can’t find an easy way to get only the weekday names.
I thought I could add some days to the EPOCH time, to reach the first day of the week, but I’m failing to find a formatter printing just the weekday.
JavaScript
1
9
1
var date = new Date(0);
2
date.setDate(4 + day);
3
for (var i = 0; i < 7; i++) {
4
var weekday = new Intl.DateTimeFormat(["en"], {
5
weekday: "short" // ?? what should I put here
6
}).format(date);
7
console.log(weekday);
8
}
9
Output:
JavaScript
1
8
1
Sunday, January 4, 1970
2
Monday, January 5, 1970
3
Tuesday, January 6, 1970
4
Wednesday, January 7, 1970
5
Thursday, January 8, 1970
6
Friday, January 9, 1970
7
Saturday, January 10, 1970
8
Desired output:
JavaScript
1
8
1
Sunday
2
Monday
3
Tuesday
4
Wednesday
5
Thursday
6
Friday
7
Saturday
8
I also would like to have a shorter version of the days, such as Sun, Mon, Tue...
.
Alternatively, is there a way to get the weekday strings from Intl
? I tried to explore the object via console but I couldn’t find them.
Advertisement
Answer
I was misled because I was using the Intl polyfill, which does not support yet { weekday: "short" }
as option.
Using native Intl implementations works as expected.