国际化 APIIntl对象是 ECMAScript 国际化规范提供的原生工具用于处理日期、时间、数字、货币、列表、相对时间、复数规则等与语言文化相关的格式化。它的最大优势是无需第三方库且能自动适配不同地区locale的格式习惯如千位分隔符、日期顺序、货币符号位置等。下面分模块详细展开并附上代码示例。一、Intl对象概述Intl是一个全局对象包含多个构造函数和方法核心有Intl.DateTimeFormat日期时间格式化Intl.NumberFormat数字含货币、百分比、科学计数格式化Intl.RelativeTimeFormat相对时间如“3 天前”Intl.ListFormat列表格式化如“A、B 和 C”Intl.PluralRules单复数规则Intl.Collator字符串比较排序每个构造函数接受两个主要参数locales地区标识符字符串或数组如zh-CN、en-US、ja-JP。可选默认使用运行环境的默认地区。options格式化选项对象控制输出的细节。二、Intl.DateTimeFormat– 日期时间格式化基础用法constdatenewDate(2026-04-28T15:30:00);// 中文简体中国格式constdtZhnewIntl.DateTimeFormat(zh-CN);console.log(dtZh.format(date));// 2026/4/28// 美国英语格式constdtEnnewIntl.DateTimeFormat(en-US);console.log(dtEn.format(date));// 4/28/2026// 日本格式constdtJanewIntl.DateTimeFormat(ja-JP);console.log(dtJa.format(date));// 2026/4/28主要选项optionsconstoptions{dateStyle:full,// full | long | medium | shorttimeStyle:medium,// 同上weekday:long,// narrow | short | longyear:numeric,// 2-digit | numericmonth:long,// numeric | 2-digit | short | longday:numeric,hour:2-digit,minute:2-digit,second:2-digit,hour12:true,// 是否使用12小时制timeZone:Asia/Shanghai,};constformatternewIntl.DateTimeFormat(zh-CN,options);console.log(formatter.format(date));// 输出示例2026年4月28日星期二 下午03:30:00formatToParts– 获取格式化后的各段内容constpartsdtZh.formatToParts(date);// [{type:year, value:2026}, {type:month, value:4}, ...]使用Intl.DateTimeFormat快速格式化内置方法也可以直接使用date.toLocaleString()它在内部调用了Intl.DateTimeFormatconsole.log(date.toLocaleString(zh-CN,{dateStyle:long}));// 2026年4月28日三、Intl.NumberFormat– 数字、货币、百分比基础用法constnum1234567.89;newIntl.NumberFormat(en-US).format(num);// 1,234,567.89newIntl.NumberFormat(de-DE).format(num);// 1.234.567,89newIntl.NumberFormat(zh-CN).format(num);// 1,234,567.89主要选项constoptions{style:currency,// decimal(默认) | currency | percent | unitcurrency:CNY,// ISO 货币代码如 USD, EUR, JPY, CNYcurrencyDisplay:symbol,// symbol | code | nameminimumFractionDigits:2,maximumFractionDigits:2,useGrouping:true,// 是否使用千位分隔符notation:compact,// standard | scientific | engineering | compactcompactDisplay:short,// short (1.2万) | long (1.2万)};示例// 货币console.log(newIntl.NumberFormat(zh-CN,{style:currency,currency:USD}).format(1234.5));// US$1,234.50// 百分比console.log(newIntl.NumberFormat(zh-CN,{style:percent}).format(0.1234));// 12%// 紧凑显示单位后缀console.log(newIntl.NumberFormat(zh-CN,{notation:compact}).format(12500));// 1.25万四、Intl.RelativeTimeFormat– 相对时间基础用法constrtfnewIntl.RelativeTimeFormat(zh-CN,{numeric:auto});console.log(rtf.format(-1,day));// 昨天console.log(rtf.format(3,day));// 3天后console.log(rtf.format(10,minute));// 10分钟后可用的时间单位year,quarter,month,week,day,hour,minute,second。选项numeric:always总是用数字如“1天前”或auto可能用“昨天”、“明天”。style:long“3 days ago”、short“3 days ago” 较短、narrow。实际应用计算距今天数functiontimeAgo(date){constrtfnewIntl.RelativeTimeFormat(zh-CN,{numeric:auto});constdiffDaysMath.round((date-newDate())/(1000*60*60*24));returnrtf.format(diffDays,day);}console.log(timeAgo(newDate(2026-04-26)));// 2天前取决于今天日期五、Intl.ListFormat– 列表连接基础用法constlist[苹果,香蕉,橘子];constlfnewIntl.ListFormat(zh-CN,{style:long,type:conjunction});console.log(lf.format(list));// 苹果、香蕉和橘子选项style:long“A, B, and C”、short、narrow通常无逗号。type:conjunction“和”、disjunction“或”、unit“米/秒”风格。六、Intl.PluralRules– 单复数判定主要用于根据数字选择正确词形如英语中的 1 book / 2 books。中文场景相对简单但对某些语言必须。constprnewIntl.PluralRules(en-US);console.log(pr.select(1));// oneconsole.log(pr.select(2));// other七、Intl.Collator– 语言敏感的字符串比较排序constnames[张三,李四,王五,陈六];constcollatornewIntl.Collator(zh-CN,{sensitivity:base});names.sort(collator.compare);console.log(names);// 按拼音排序八、关键知识点与注意事项1. 地区标识符locale标准格式语言-国家/地区如zh-CN、en-GB、zh-TW。可以使用-u-扩展指定更多细则如zh-CN-u-ca-chinese使用农历。2. 性能影响通常每次构造Intl对象有一定开销。如果频繁使用应将构造好的实例缓存起来复用。3. 浏览器兼容性主流现代浏览器和 Node.js 均完美支持。老旧浏览器如 IE11不支持但如今基本可忽略。4. 与Date.prototype.toLocaleString/Number.prototype.toLocaleString的关系这些原型方法是IntlAPI 的简易封装。两者选其一均可但Intl构造函数更清晰地隔离配置且在需要多次调用同一格式时性能更好复用实例。九、实际项目组合示例// 格式化当前时间 金额总计constnownewDate();constprice12345.6;constdateFormatternewIntl.DateTimeFormat(zh-CN,{dateStyle:full,timeStyle:medium});constmoneyFormatternewIntl.NumberFormat(zh-CN,{style:currency,currency:CNY});console.log(订单时间${dateFormatter.format(now)});console.log(总金额${moneyFormatter.format(price)});// 输出示例// 订单时间2026年4月28日星期二 下午03:30:00// 总金额¥12,345.60总结Intl对象是 JavaScript 原生国际化方案的基石它消除了手工拼接数字、日期的繁琐。自动处理不同地区文化习惯尽管中文和英文习惯差异不大但对德、法、阿拉伯语等差异很大。无需引入 moment.js / numeral.js 等库性能更好体积更小。在你的日常开发中如果需要跨地区展示或严格遵循本地化格式应优先使用IntlAPI。对于简单的不涉及本地化的场景如仅拼接字符串模板字符串仍然是最轻量的选择。如果还有关于某个具体构造函数或选项的疑问欢迎继续提问