学完这篇,你能直接用浏览器和 Node 里内置的 Intl 对象把日期、数字、货币、相对时间格式化成任意语言和地区的样式,不用再手写拼接字符串,也不用为这点事引入 dayjs、numeral 之类的库。
第一步:认识 Intl 提供的能力
Intl 是 ECMAScript 内置的全局对象,不需要安装、不需要引入,Intl 上挂着一批构造器:
Intl.DateTimeFormat:日期时间格式化
Intl.NumberFormat:数字、货币、百分比、单位
Intl.RelativeTimeFormat:相对时间(“3 天前”)
Intl.ListFormat:列表连接(“A、B和C”)
Intl.Collator:按语言规则排序比较
Intl.PluralRules:复数形式判断
它们的用法高度统一:new Intl.XXX(locale, options),然后调用 .format(值)。locale 传 'zh-CN'、'en-US'、'ja-JP' 这样的 BCP 47 标签,也可以传数组做优先级回退:['zh-CN', 'zh', 'en']。
第二步:格式化日期时间
最省事的写法是用 dateStyle / timeStyle 预设:
const d = new Date('2025-03-08T14:30:00Z');
new Intl.DateTimeFormat('zh-CN', { dateStyle: 'long' }).format(d);
// 2025年3月8日
new Intl.DateTimeFormat('zh-CN', { dateStyle: 'medium', timeStyle: 'short' }).format(d);
// 2025年3月8日 22:30
new Intl.DateTimeFormat('en-US', { dateStyle: 'full' }).format(d);
// Saturday, March 8, 2025
需要精细控制时改成具体字段:year、month、day、weekday、hour、minute、second、timeZoneName,每个字段可取值 'numeric'、'2-digit'、'long'、'short'、'narrow'。
注意:dateStyle / timeStyle 不能和 year、month 这类单字段混用,同时传会直接抛 TypeError。两种风格二选一。
时区默认取运行环境的时区,服务器上通常是 UTC,直接格式化会出现“差 8 小时”的错觉。要固定时区就显式指定:
new Intl.DateTimeFormat('zh-CN', {
dateStyle: 'short',
timeStyle: 'short',
timeZone: 'Asia/Shanghai'
}).format(d);
第三步:格式化数字与货币
const nf = new Intl.NumberFormat('zh-CN');
nf.format(1234567.891); // 1,234,567.891
new Intl.NumberFormat('zh-CN', { style: 'percent' }).format(0.256);
// 26%
new Intl.NumberFormat('zh-CN', {
style: 'currency', currency: 'CNY'
}).format(1299.5);
// ¥1,299.50
new Intl.NumberFormat('zh-CN', {
style: 'unit', unit: 'kilobyte', unitDisplay: 'short'
}).format(2048);
// 2,048 kB
new Intl.NumberFormat('zh-CN', {
notation: 'compact', maximumFractionDigits: 1
}).format(123456);
// 12.3万
小数位数由 minimumFractionDigits / maximumFractionDigits 控制,整数部分用 minimumIntegerDigits,是否分组用 useGrouping。
注意:currency 必须是 ISO 4217 的三位字母代码(CNY、USD、JPY)。传 '¥'、'RMB' 会抛 RangeError。
第四步:相对时间与列表拼接
const rtf = new Intl.RelativeTimeFormat('zh-CN', { numeric: 'auto' });
rtf.format(-1, 'day'); // 昨天
rtf.format(3, 'hour'); // 3小时后
第二个参数是单位,可选 'second'、'minute'、'hour'、'day'、'week'、'month'、'year' 等;数值为负表示过去。numeric: 'auto' 才会输出“昨天”这类习惯说法,默认 'always' 会输出“1天前”。
new Intl.ListFormat('zh-CN', { type: 'conjunction' }).format(['帖子', '用户', '附件']);
// 帖子、用户和附件
type 可以换成 'disjunction'(“或”)或 'unit'(不带连接词)。
第五步:缓存实例,别在循环里 new
new Intl.NumberFormat(...) 每次构造都要做一次 locale 解析和 ICU 规则查找,比 format() 本身贵得多。在渲染列表、循环格式化时,把实例提到外面或用 Map 缓存:
const cache = new Map();
function getNF(locale, opts) {
const key = locale + JSON.stringify(opts);
if (!cache.has(key)) cache.set(key, new Intl.NumberFormat(locale, opts));
return cache.get(key);
}
另外,Intl 还提供了 formatToParts(),返回 [{type:'integer', value:'1'}, ...] 这样的分段数组,需要给某一段单独加样式(比如整数放大、小数变小)时用它,比正则拆字符串稳。
注意:Intl 只负责“格式化”,不负责“解析”。没有 parse,别指望把 "2025年3月8日" 反解成 Date。解析仍要靠 Date 构造器或新标准 Temporal(浏览器支持尚不完整)。
第六步:处理 locale 回退与兼容
用户语言一般取 navigator.language,但它可能是 zh-TW、en-GB 这类你没覆盖到的值。传数组让运行时自己挑最匹配的:
const locale = ['zh-CN', 'zh', 'en'].find(
l => Intl.DateTimeFormat.supportedLocalesOf(l).length > 0
) || 'zh-CN';
语言不支持时 Intl 不会报错,而是静默回退到默认 locale,所以最好用 supportedLocalesOf() 先探测一下,避免界面出现预期外的语言。
小结
Intl 是内置对象,零依赖,new Intl.XXX(locale, options).format(value) 是统一套路。
- 日期用
dateStyle / timeStyle 快速出结果,但要和单字段配置二选一;跨时区场景务必显式写 timeZone。
- 数字靠
style 切换 decimal / percent / currency / unit,大数用 notation: 'compact';货币代码必须是三位 ISO 码。
- 相对时间用
RelativeTimeFormat + numeric: 'auto',列表拼接用 ListFormat。
- 性能关键是缓存 formatter 实例,需要局部样式时用
formatToParts()。
Intl 只格式化不解析,locale 选不到会静默回退,用 supportedLocalesOf() 兜底。