JavaScript的IntlAPI是ECMAScript标准内置的国际化对象,提供了一系列用于字符串比较、数字格式化、日期时间格式化的构造函数,能够帮助开发者快速实现应用的多地区适配,无需依赖第三方国际化库。

Intl.DateTimeFormat:日期时间格式化
Intl.DateTimeFormat用于处理日期和时间的本地化格式化,第一个参数是语言标签字符串或数组,第二个参数是配置选项对象。
// 基础用法:格式化当前时间为中文简体格式
const now = new Date();
const cnDateFormatter = new Intl.DateTimeFormat('zh-CN');
console.log(cnDateFormatter.format(now)); // 输出类似 2024/5/20
// 自定义配置:展示完整日期和时间
const fullDateFormatter = new Intl.DateTimeFormat('zh-CN', {
year: 'numeric',
month: 'long',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false
});
console.log(fullDateFormatter.format(now)); // 输出类似 2024年5月20日 14:30:00
// 适配不同地区格式
const usDateFormatter = new Intl.DateTimeFormat('en-US');
console.log(usDateFormatter.format(now)); // 输出类似 5/20/2024Intl.NumberFormat:数字与货币格式化
Intl.NumberFormat可以处理普通数字、百分比、货币的本地化展示,支持配置小数位数、分组分隔符等规则。
const num = 1234567.89;
// 普通数字格式化,中文环境下使用逗号作为千分位分隔符
const cnNumFormatter = new Intl.NumberFormat('zh-CN');
console.log(cnNumFormatter.format(num)); // 输出 1,234,567.89
// 货币格式化,人民币显示为¥符号
const rmbFormatter = new Intl.NumberFormat('zh-CN', {
style: 'currency',
currency: 'CNY'
});
console.log(rmbFormatter.format(num)); // 输出 ¥1,234,567.89
// 百分比格式化
const percentFormatter = new Intl.NumberFormat('zh-CN', {
style: 'percent',
minimumFractionDigits: 2
});
console.log(percentFormatter.format(0.1234)); // 输出 12.34%
// 美元货币适配
const usdFormatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD'
});
console.log(usdFormatter.format(num)); // 输出 $1,234,567.89Intl.Collator:字符串比较与排序
Intl.Collator用于按照本地化规则进行字符串比较和排序,支持配置大小写敏感度、重音符号处理等规则。
const words = ['apple', 'Banana', 'cherry', 'Date'];
// 默认排序,区分大小写
const defaultCollator = new Intl.Collator('en-US');
console.log([...words].sort(defaultCollator.compare));
// 输出 ["Banana", "Date", "apple", "cherry"]
// 不区分大小写排序
const caseInsensitiveCollator = new Intl.Collator('en-US', {
sensitivity: 'base'
});
console.log([...words].sort(caseInsensitiveCollator.compare));
// 输出 ["apple", "Banana", "cherry", "Date"]
// 中文排序,按拼音顺序
const cnWords = ['张三', '李四', '王五', '赵六'];
const cnCollator = new Intl.Collator('zh-CN');
console.log([...cnWords].sort(cnCollator.compare));
// 输出 ["李四", "王五", "张三", "赵六"]其他常用Intl接口
除了上述三个核心接口,Intl还提供了其他实用接口:
Intl.ListFormat:用于格式化列表,比如将数组转为自然语言的列表字符串,支持"and""or"等连接词配置Intl.RelativeTimeFormat:用于格式化相对时间,比如"3天前""2小时后"这类表述Intl.PluralRules:用于根据数量选择对应的复数形式,适配不同语言的复数规则
// Intl.RelativeTimeFormat示例
const relativeFormatter = new Intl.RelativeTimeFormat('zh-CN', {
numeric: 'auto'
});
console.log(relativeFormatter.format(-3, 'day')); // 输出 前天
console.log(relativeFormatter.format(2, 'hour')); // 输出 2小时后
// Intl.PluralRules示例
const pluralRules = new Intl.PluralRules('en-US');
console.log(pluralRules.select(1)); // 输出 one
console.log(pluralRules.select(2)); // 输出 other使用注意事项
在使用IntlAPI时需要注意以下几点:
- IntlAPI是ECMAScript标准内置对象,现代浏览器和Node.js环境都原生支持,无需额外安装依赖
- 语言标签需要符合BCP 47规范,比如
zh-CN代表中文简体,en-US代表美国英语 - 如果传入的语言标签不被环境支持,Intl会使用最接近的可用语言作为 fallback,不会直接报错
- 对于性能敏感的场景,可以缓存创建好的Intl实例,避免重复初始化带来的开销
Intl_APIJavaScript国际化格式化本地化修改时间:2026-06-03 00:45:46