diff --git a/js/2024-1-9.intl.md b/js/2024-1-9.intl.md index 44c75bf8..d9d04194 100644 --- a/js/2024-1-9.intl.md +++ b/js/2024-1-9.intl.md @@ -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的星期一。 @@ -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))); @@ -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))); @@ -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 -``` \ No newline at end of file +```