Intl.DateTimeFormat通过locales和options参数实现多语言日期时间格式化,支持不同地区、历法与时区。它利用ICU数据自动处理日期顺序、名称翻译及数字系统,并可通过calendar和timeZone选项处理日本历、伊斯兰历及时区转换;formatToParts()支持精细化控制,而缓存实例可提升性能,兼容性方面需注意老旧浏览器polyfill及Node.js的ICU数据完整性。

JavaScript的
Intl.DateTimeFormat
Intl.DateTimeFormat
locales
options
locales
'en-US'
'zh-CN'
'de-DE'
options
举个例子,创建一个格式化器:
const date = new Date(); // 假设现在是 2023年10月27日 下午3点45分 (UTC+8)
// 基本用法:根据语言环境自动格式化
let formatter = new Intl.DateTimeFormat('en-US');
console.log(formatter.format(date)); // 10/27/2023
formatter = new Intl.DateTimeFormat('zh-CN');
console.log(formatter.format(date)); // 2023/10/27
// 带选项的用法:指定日期和时间样式
formatter = new Intl.DateTimeFormat('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
});
console.log(formatter.format(date)); // October 27, 2023 at 03:45:00 PM (假设时区是美国东部时间)
// 更简洁的方式:使用 dateStyle 和 timeStyle
formatter = new Intl.DateTimeFormat('en-US', {
dateStyle: 'full',
timeStyle: 'long'
});
console.log(formatter.format(date)); // Friday, October 27, 2023 at 3:45:00 PM GMT+8 (实际输出会根据运行环境的时区而定)Intl.DateTimeFormat
当我们谈论多语言日期和时间显示时,
Intl.DateTimeFormat
立即学习“Java免费学习笔记(深入)”;
例如,一个日期
2023年10月27日
en-US
10/27/2023
October 27, 2023
de-DE
27.10.2023
zh-CN
2023/10/27
2023年10月27日
Intl.DateTimeFormat
locales
const myDate = new Date(2023, 9, 27); // 注意月份是0-11,所以9代表10月
console.log(new Intl.DateTimeFormat('en-US').format(myDate)); // 10/27/2023
console.log(new Intl.DateTimeFormat('de-DE').format(myDate)); // 27.10.2023
console.log(new Intl.DateTimeFormat('zh-CN').format(myDate)); // 2023/10/27
// 还可以指定更详细的格式
console.log(new Intl.DateTimeFormat('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric'
}).format(myDate)); // October 27, 2023
console.log(new Intl.DateTimeFormat('zh-CN', {
year: 'numeric',
month: 'long',
day: 'numeric'
}).format(myDate)); // 2023年10月27日
// 如果你需要对日期各个部分进行单独处理,比如自定义样式,可以使用 formatToParts()
const parts = new Intl.DateTimeFormat('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric'
}).formatToParts(myDate);
console.log(parts.map(p => `${p.type}: ${p.value}`).join(', '));
// year: 2023, literal: , month: October, literal: , day: 27formatToParts()
year
month
day
Intl.DateTimeFormat
处理历法和时区差异是国际化日期格式化的真正挑战所在,而
Intl.DateTimeFormat
calendar
历法(Calendar)处理: 除了我们常用的公历(Gregorian calendar),世界上还有许多其他历法,比如伊斯兰历、希伯来历、日本历、农历等。
Intl.DateTimeFormat
calendar
const specificDate = new Date(2023, 9, 27); // 2023年10月27日
// 使用日本历 (Japanese calendar)
let formatterJapanese = new Intl.DateTimeFormat('ja-JP-u-ca-japanese', {
year: 'numeric',
month: 'long',
day: 'numeric',
era: 'long' // 日本历会显示年号
});
console.log(formatterJapanese.format(specificDate)); // 令和5年10月27日
// 使用伊斯兰历 (Islamic calendar)
let formatterIslamic = new Intl.DateTimeFormat('ar-SA-u-ca-islamic', {
year: 'numeric',
month: 'long',
day: 'numeric'
});
console.log(formatterIslamic.format(specificDate)); // ١٤٤٥ ربيع الآخر ١٢ (1445年,第二个月,第12天)这里需要注意的是,
u-ca-japanese
Intl.DateTimeFormat
时区(Time Zone)处理: 时区是另一个让开发者头疼的问题。我发现很多新手开发者会在这里踩坑,以为只要设置了
new Date()
new Date()
Intl.DateTimeFormat
timeZone
const utcDate = new Date('2023-10-27T15:00:00Z'); // UTC时间 2023年10月27日 下午3点
// 显示为纽约时间 (America/New_York)
let nyFormatter = new Intl.DateTimeFormat('en-US', {
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
timeZone: 'America/New_York',
timeZoneName: 'short'
});
console.log(nyFormatter.format(utcDate)); // 11:00:00 AM EDT (纽约在夏令时)
// 显示为上海时间 (Asia/Shanghai)
let shFormatter = new Intl.DateTimeFormat('zh-CN', {
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
timeZone: 'Asia/Shanghai',
timeZoneName: 'short'
});
console.log(shFormatter.format(utcDate)); // 下午11:00:00 GMT+8
// 如果不指定 timeZone,会使用运行环境的默认时区
let defaultFormatter = new Intl.DateTimeFormat('en-US', {
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
});
console.log(defaultFormatter.format(utcDate)); // 实际输出取决于你运行代码的机器的时区关键点在于,内部存储日期时最好使用UTC时间(
new Date()
Date.UTC()
timeZoneName
EDT
GMT+8
Intl.DateTimeFormat
尽管
Intl.DateTimeFormat
Intl
浏览器支持: 现代主流浏览器(Chrome、Firefox、Safari、Edge)对
Intl.DateTimeFormat
options
Intl.js
caniuse.com
Node.js环境: 在Node.js环境中,
Intl
full-icu
--icu-data-dir
node-full-icu
npm install full-icu node --icu-data-dir=./node_modules/full-icu your_script.js
缺失的Locale/Calendar/TimeZone数据: 即使在支持
Intl
locale
calendar
timeZone
Intl.DateTimeFormat
Intl.DateTimeFormat.supportedLocalesOf()
const requestedLocales = ['fr-FR', 'xyz-XYZ']; // 'xyz-XYZ' 是一个不存在的语言环境 const supported = Intl.DateTimeFormat.supportedLocalesOf(requestedLocales); console.log(supported); // ['fr-FR']
对于生产环境,我们通常会有一个回退策略,比如如果用户请求的语言不支持,就回退到英语或其他默认语言。
性能考虑: 创建
Intl.DateTimeFormat
Intl.DateTimeFormat
locale
options
// 简单的格式化器缓存
const formatterCache = {};
function getCachedDateTimeFormatter(locale, options) {
// 创建一个唯一的键来标识这个格式化器配置
const key = `${locale}-${JSON.stringify(options)}`;
if (!formatterCache[key]) {
formatterCache[key] = new Intl.DateTimeFormat(locale, options);
}
return formatterCache[key];
}
const longDateOptions = { year: 'numeric', month: 'long', day: 'numeric' };
const date1 = new Date();
const date2 = new Date(date1.getTime() + 86400000); // 明天
// 第一次创建并缓存
const enUsFormatter = getCachedDateTimeFormatter('en-US', longDateOptions);
console.log(enUsFormatter.format(date1));
// 第二次直接从缓存获取
console.log(getCachedDateTimeFormatter('en-US', longDateOptions).format(date2));
// 切换语言,创建新的格式化器并缓存
const zhCnFormatter = getCachedDateTimeFormatter('zh-CN', longDateOptions);
console.log(zhCnFormatter.format(date1));通过这种方式,可以显著减少重复创建对象带来的性能损耗,让你的国际化应用更加流畅。
以上就是如何利用JavaScript的Intl.DateTimeFormat实现多语言日期格式化,以及它如何处理历法和时区差异?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号