You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何仅用Intl.DateTimeFormat()实现DD.MM.YYYY格式日期?

如何仅通过Intl.DateTimeFormat()获取DD.MM.YYYY格式的日期?

当然可以,不需要借助replace()或其他字符串操作方法,有两种可靠的实现方式:

方法1:使用原生支持DD.MM.YYYY格式的区域设置

很多地区的默认日期格式就是DD.MM.YYYY,比如德语区(de-DE)、荷兰语区(nl-NL)等,直接指定对应的区域设置即可:

const formattedDate = new Intl.DateTimeFormat('de-DE').format(new Date());
// 输出示例:24.04.2023

这种方式最简单,无需额外配置,直接利用Intl的内置区域格式化规则。

方法2:通过formatToParts()手动组装格式

如果不想依赖特定区域设置,可以用formatToParts()方法拆分日期的各个部分,再按照DD.MM.YYYY的顺序用点连接:

const date = new Date();
const dateParts = new Intl.DateTimeFormat('en-US', {
  day: '2-digit',
  month: '2-digit',
  year: 'numeric'
}).formatToParts(date);

// 提取各部分值
const day = dateParts.find(part => part.type === 'day').value;
const month = dateParts.find(part => part.type === 'month').value;
const year = dateParts.find(part => part.type === 'year').value;

// 组装成目标格式
const formattedDate = `${day}.${month}.${year}`;
// 输出示例:24.04.2023

这种方式更灵活,完全自定义日期各部分的顺序和分隔符,不依赖任何区域设置的默认格式。

内容的提问来源于stack exchange,提问作者Amir Mohammad Moradi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 02:22:15