一、Intl.DateTimeFormat 核心概念
Intl.DateTimeFormat 是 ES6 新增的原生国际化 API,专门用于语言敏感的日期和时间格式化,由浏览器原生实现,无需引入第三方库,是前端处理多语言日期格式化的首选方案。
1.1 基本语法
new Intl.DateTimeFormat(locales, options)
locales:字符串 / 字符串数组,指定语言 / 地区(如 zh-CN、en-US、ja-JP),支持 BCP 47 语言标签。
options:配置对象,控制日期 / 时间的显示格式(可选)。
1.2 核心参数(options)详解
基础参数
日期时间组件参数
格式参数
二、代码示例
2.1 基础用法(默认格式)
// 当前时间,默认语言(浏览器语言),默认格式
const date = new Date(2026, 0, 8, 14, 30, 5); // 2026-01-08 14:30:05
const formatter = new Intl.DateTimeFormat();
console.log(formatter.format(date)); // 输出(zh-CN 环境):2026/1/8
2.2 指定语言和基础日期格式
const date = new Date(2026, 0, 8); // 中文(完整格式)
const cnFormatter = new Intl.DateTimeFormat('zh-CN', {year:'numeric', month:'long', day:'numeric', weekday:'long'});
console.log(cnFormatter.format(date)); // 2026 年 1 月 8 日星期四
// 英文(美式)
const enFormatter = new Intl.DateTimeFormat('en-US', {year:'numeric', month:'short', day:'2-digit', weekday:'short'});
console.log(enFormatter.format(date)); // Thu, Jan 08, 2026
2.3 包含时间和时区的格式
const date = new Date(2026, 0, 8, 14, 30, 5); // 北京时间(东八区)+ 12 小时制
const beijingFormatter = new Intl.DateTimeFormat('zh-CN', {timeZone:'Asia/Shanghai', hour12:true, year:'numeric', month:'2-digit', day:'2-digit', hour:'2-digit', minute:'2-digit', second:'2-digit'});
console.log(beijingFormatter.format(date)); // 2026/01/08 下午 02:30:05
// UTC 时区
const utcFormatter = new Intl.DateTimeFormat('en-US', {timeZone:'UTC', hour12:false, hour:'2-digit', minute:'2-digit'});
console.log(utcFormatter.format(date)); // 06:30(UTC 比北京时间慢 8 小时)
2.4 进阶:获取格式化后的各部分(formatToParts)
const date = new Date(2026, 0, 8);
const formatter = new Intl.DateTimeFormat('zh-CN', { year:'numeric', month:'long', day:'numeric'});
// 拆分格式化后的各个部分(便于自定义拼接)
const parts = formatter.formatToParts(date);
console.log(parts); // 输出:// [// { type: 'year', value: '2026' },// { type: 'literal', value: '年' },// { type: 'month', value: '1 月' },// { type: 'literal', value: '' },// { type: 'day', value: '8 日' }// ]
// 自定义拼接:2026-01-08
const customFormat = parts.reduce((res, part)=>{
if(part.type ==='year') res += part.value;
if(part.type ==='month') res +='-'+ part.value.replace('月','').padStart(2,'0');
if(part.type ==='day') res +='-'+ part.value.replace('日','').padStart(2,'0');
return res;},'');
console.log(customFormat); // 2026-01-08
三、注意事项
- 月份是 1 基:JavaScript Date 对象的月份是 0-11(0=1 月,11=12 月),格式化时会自动转换为 1-12,无需手动 + 1。
- 时区参数有效性:timeZone 必须是 IANA 时区(如 Asia/Shanghai),不能写 GMT+8 或 UTC+8,否则会忽略该参数。
- 浏览器兼容性:
现代浏览器(Chrome/Firefox/Safari 10+/Edge)完全支持;
IE 11 仅支持基础功能,部分参数(如 weekday、era)不兼容,需做降级处理。 - 默认值规则:如果只配置 time 相关参数(如 hour),会默认显示时间;如果只配置 date 相关参数,会默认显示日期;如果无配置,显示「年 / 月 / 日」(随语言)。
- 性能:频繁创建 Intl.DateTimeFormat 实例会消耗性能,建议复用实例(如缓存到变量中)。
四、优缺点分析
优点
- 原生无依赖:无需引入 moment.js、dayjs 等库,减小包体积。
- 国际化友好:支持全球所有语言 / 地区的日期格式,自动适配地区习惯(如中文「年 / 月 / 日」、英文「月 / 日 / 年」)。
- 时区支持:原生支持 IANA 时区,无需手动计算时区偏移。
- 性能优秀:原生 API 执行效率远高于第三方库的纯 JS 实现。
- 灵活拆分:formatToParts 方法可拆分格式化后的各部分,便于自定义样式 / 拼接。
缺点
- API 较繁琐:配置参数多,简单格式化(如 YYYY-MM-DD)需要写较多配置。
- 兼容性问题:IE 11 支持不完整,老旧项目需兼容处理。
- 自定义格式能力弱:无法直接通过「模板字符串」(如 YYYY-MM-DD HH:mm)快速格式化,需通过 formatToParts 手动拼接。
- 缺乏相对时间:不支持「3 天前」「1 小时后」等相对时间格式化,需额外实现。
五、其他日期格式化方案
5.1 手动拼接(纯原生 JS)
适用场景:简单格式、无需国际化、兼容老旧浏览器
function formatDate(date){
const year = date.getFullYear();
const month = String(date.getMonth()+1).padStart(2,'0'); // 补零
const day = String(date.getDate()).padStart(2,'0');
const hour = String(date.getHours()).padStart(2,'0');
const minute = String(date.getMinutes()).padStart(2,'0');
return `${year}-${month}-${day} ${hour}:${minute}`;
}
console.log(formatDate(new Date(2026, 0, 8, 14, 30))); // 2026-01-08 14:30
优缺点:简单灵活,但不支持国际化、时区,需手动处理补零 / 时区偏移,代码冗余。
5.2 dayjs(轻量级第三方库)
简介:替代 moment.js 的轻量库(体积~2KB),API 简洁,支持插件扩展。
安装:
npm install dayjs
示例:
import dayjs from 'dayjs';
// 引入国际化插件
import 'dayjs/locale/zh-cn';
// 引入时区插件
import utc from 'dayjs/plugin/utc';
import timezone from 'dayjs/plugin/timezone';
dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.locale('zh-cn'); // 设置中文
// 基础格式化
console.log(dayjs('2026-01-08').format('YYYY-MM-DD HH:mm:ss')); // 2026-01-08 00:00:00
// 时区格式化
console.log(dayjs('2026-01-08 14:30').tz('Asia/Shanghai').format('YYYY-MM-DD HH:mm')); // 2026-01-08 14:30
console.log(dayjs('2026-01-08 14:30').tz('UTC').format('YYYY-MM-DD HH:mm')); // 2026-01-08 06:30
// 相对时间
console.log(dayjs('2026-01-08').fromNow()); // (根据当前时间)X 天后
优缺点:体积小、API 友好、支持国际化 / 时区 / 相对时间;需引入第三方库,增加包体积(但远小于 moment.js)。
5.3 moment.js(经典但笨重)
简介:老牌日期处理库,功能全面,但体积大(~200KB),已进入维护模式(不再新增功能)。
示例:
import moment from 'moment';
import 'moment-timezone';
// 格式化
console.log(moment('2026-01-08').format('YYYY 年 MM 月 DD 日')); // 2026 年 01 月 08 日
// 时区
console.log(moment.tz('2026-01-08 14:30','Asia/Shanghai').utc().format('YYYY-MM-DD HH:mm')); // 2026-01-08 06:30
优缺点:功能最全、文档丰富;体积大、性能一般、已停止维护,不建议新项目使用。
六、总结与建议
总结
- Intl.DateTimeFormat 是原生 API,无依赖、性能好、国际化友好,但配置繁琐、自定义格式能力弱;
- 手动拼接适合简单场景,无依赖但不支持国际化 / 时区;
- dayjs 是轻量级第三方方案,平衡了功能和体积,是当前主流选择;
- moment.js 功能全但笨重,仅建议维护老项目时使用。
建议
新项目优先选择:
- 仅需简单格式化(如 YYYY-MM-DD)且无需国际化:手动拼接 + padStart 补零;
- 需要国际化 / 时区:Intl.DateTimeFormat(原生)或 dayjs(轻量库);
- 需要相对时间 / 复杂计算:dayjs(推荐)。
兼容性处理:
- 若需兼容 IE 11,优先使用 dayjs,或为 Intl.DateTimeFormat 做降级(如手动拼接);
性能优化:
- 复用 Intl.DateTimeFormat 实例,避免频繁创建;
- 避免在循环中重复格式化日期。
避坑提醒:
- 不要混用不同日期库(如 moment.js + dayjs),避免类型冲突;
- 处理时区时,统一使用 IANA 时区(如 Asia/Shanghai),采用'区域/城市'格式,而非 GMT 偏移量。


