Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions js/2024-1-9.intl.md
Original file line number Diff line number Diff line change
Expand Up @@ -1205,14 +1205,14 @@ en-US一般每周第一天是周日,而zh-CN一般每周第一天是周一。


```jsx
(new Intl.Locale('zh-CN')).weekInfo // {"firstDay":1,"weekend":[6,7],"minimalDays":1}
(new Intl.Locale('en-US')).weekInfo // {"firstDay":7,"weekend":[6,7],"minimalDays":1}
(new Intl.Locale('zh-CN')).getWeekInfo() // {"firstDay":1,"weekend":[6,7],"minimalDays":1}
(new Intl.Locale('en-US')).getWeekInfo() // {"firstDay":7,"weekend":[6,7],"minimalDays":1}

const locale = 'zh-CN'
console.log(new Intl.DateTimeFormat(locale, { weekday: "long" }).format(new Date(0, 0, new Intl.Locale(locale).weekInfo.firstDay))) // '星期一'
console.log(new Intl.DateTimeFormat(locale, { weekday: "long" }).format(new Date(0, 0, new Intl.Locale(locale).getWeekInfo().firstDay))) // '星期一'

const locale = 'en-US'
console.log(new Intl.DateTimeFormat(locale, { weekday: "long" }).format(new Date(0, 0, new Intl.Locale(locale).weekInfo.firstDay))) // 'Sunday'
console.log(new Intl.DateTimeFormat(locale, { weekday: "long" }).format(new Date(0, 0, new Intl.Locale(locale).getWeekInfo().firstDay))) // 'Sunday'
```

能生效的原因是 `new Date(0,0,7)` 等价于 `new Date(1900,0,7)` 对应1900年1月7日(UTC+0),此时对应US的Sunday。而 `new Date(0,0,1)` 对应1900年1月1日(UTC+0),对应CN的星期一。
Expand All @@ -1229,15 +1229,15 @@ console.log(new Intl.DateTimeFormat(locale, { weekday: "long" }).format(new Date
1. 如何获取一周的第一天
```JS
const localeUS = 'en-US'
new Intl.DateTimeFormat(localeUS, { weekday: "long" }).format(new Date(0, 0, new Intl.Locale(localeUS).weekInfo.firstDay)) // 'Sunday'
new Intl.DateTimeFormat(localeUS, { weekday: "long" }).format(new Date(0, 0, new Intl.Locale(localeUS).getWeekInfo().firstDay)) // 'Sunday'

const localeCN = 'zh-CN'
new Intl.DateTimeFormat(localeCN, { weekday: "long" }).format(new Date(0, 0, new Intl.Locale(localeCN).weekInfo.firstDay)) // '星期一'
new Intl.DateTimeFormat(localeCN, { weekday: "long" }).format(new Date(0, 0, new Intl.Locale(localeCN).getWeekInfo().firstDay)) // '星期一'
```
2. 输出一周7天
```JS
const locale = "en-US";
const firstDay = new Intl.Locale(locale).weekInfo.firstDay;
const firstDay = new Intl.Locale(locale).getWeekInfo().firstDay;
const formatInstace = new Intl.DateTimeFormat(locale, { weekday: "long" });
for (let i = 0; i < 7; i++) {
console.log(formatInstace.format(new Date(0, 0, firstDay + i)));
Expand All @@ -1251,7 +1251,7 @@ for (let i = 0; i < 7; i++) {
// Saturday

const localeCN = "zh-CN";
const firstDayCN = new Intl.Locale(localeCN).weekInfo.firstDay;
const firstDayCN = new Intl.Locale(localeCN).getWeekInfo().firstDay;
const formatInstaceCN = new Intl.DateTimeFormat(localeCN, { weekday: "long" });
for (let i = 0; i < 7; i++) {
console.log(formatInstaceCN.format(new Date(0, 0, firstDayCN + i)));
Expand Down Expand Up @@ -1424,4 +1424,4 @@ const intlEn = createIntl(
// Call imperatively
console.log(intlFr.formatNumber(2000000000000)) // 2 000 000 000 000
console.log(intlEn.formatNumber(2000000000000)) // 2,000,000,000,000
```
```