最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - First day of week from HTML5 Intl API - Stack Overflow

programmeradmin5浏览0评论

Is there a way to get the first day of the week (Sunday or Monday for most countries) from the HTML5 internationalization API?

The spec can be found here. I would be surprised if it's not somehow disclosed, but I can't seem to find where.

Is there a way to get the first day of the week (Sunday or Monday for most countries) from the HTML5 internationalization API?

The spec can be found here. I would be surprised if it's not somehow disclosed, but I can't seem to find where.

Share Improve this question edited May 27, 2015 at 22:03 TessellatingHeckler 29k4 gold badges54 silver badges93 bronze badges asked Dec 18, 2014 at 15:49 David MulderDavid Mulder 27k11 gold badges56 silver badges118 bronze badges 2
  • It looks like Intl has been designed to follow ISO 8601, which only allows Monday as first day of week :( – gpbl Commented May 28, 2015 at 17:32
  • I'm fan of Intl as implemented in PHP: a wrapper for ICU. My expectation was that browsers would also just use the ICU library from the Unicode Consortium to implement their own Intl extention. But it seems everybody implements their own? A workaround is to output getFirstDayOfWeek() in JSON to your own HTML5 app. – Code4R7 Commented Jul 10, 2017 at 14:07
Add a ment  | 

2 Answers 2

Reset to default 9

The official vanilla API for retrieving this is Locale.prototype.getWeekInfo(), or the currently more supported Locale.prototype.weekInfo (Firefox does not support either!):

Small note: Firefox does not support either!

// New API, currently only in Deno, Bun & Safari Technology Preview (2023-10)
new Intl.Locale('en-US').getWeekInfo()

// "Old" API, available in Chrome, Edge, Opera, Node & other Chromium-based browsers
new Intl.Locale('en-US').weekInfo

which will return an object like:

{
    firstDay: 7,      // First day of the week is Sunday
    minimalDays: 1,   // First calendar week of the year must have at least 1 weekday
    weekend: [6, 7]   // Weekend is Saturday and Sunday
}

Examples for other cultures: (new Intl.Locale('<locale>').weekInfo)

Germany de-DE:

{
    firstDay: 1,      // First day of the week is Monday
    minimalDays: 4,   // First calendar week of the year has at least 4 weekdays
    weekend: [6, 7]   // Weekend is Saturday and Sunday
}

Egypt ar-EG:

{
    firstDay: 6,      // First day of the week is Saturday
    minimalDays: 1,   // First calendar week of the year has at least 1 weekday
    weekend: [5, 6]   // Weekend is Friday and Saturday
}

Uganda sw-UG:

{
    firstDay: 1,      // First day of the week is Monday
    minimalDays: 1,   // First calendar week of the year has at least 1 weekday
    weekend: [7]      // Weekend is Sunday only
}

Brunei ms-BN:

{
    firstDay: 7,      // First day of the week is Sunday
    minimalDays: 1,   // First calendar week of the year has at least 1 weekday
    weekend: [5, 7]   // Weekend is Friday and Sunday
}

Polyfilling this is possible, but I remend the excellent date-fns library until the Temporal API arrives in most browsers, which has similar data (without weekend information) available for currently 93 locales:

import { enUS, deAT, ar, hi } from 'date-fns/locale'

enUS.options.weekStartsOn          // 0 <- careful, 0 = Sunday
enUS.options.firstWeekContainsDate // 1

deAT.options.weekStartsOn          // 1
deAT.options.firstWeekContainsDate // 4

// Arabic
ar.options.weekStartsOn            // 6
ar.options.firstWeekContainsDate   // 1

// Hindi
hi.options.weekStartsOn            // 0
hi.options.firstWeekContainsDate   // 4

Small Note - the spec has changed from a property to a function, since all browsers/runtimes implemented weekInfo as a getter which would always return a new object, and cause this unintuitive behavior:

const swedish = new Intl.Locale('sv');
swedish.weekInfo === swedish.weekInfo  // false

Intl doesn't have an API to access this sort of calendar information yet. It's possible it may add support for doing so in the future, but right now you're out of luck.

发布评论

评论列表(0)

  1. 暂无评论