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

JavaScript时间显示格式调整及多语言显示问题咨询

解决方案

1. 调整为期望的中文日期时间格式

你可以使用JavaScript内置的Intl.DateTimeFormat API实现所需格式,无需手动拼接补零,代码更简洁易维护。修改后的代码如下:

JavaScript代码

function getDateTime() {
  // 配置格式:年/月/日带中文单位、完整星期、24小时制时分秒
  const formatter = new Intl.DateTimeFormat('zh-CN', {
    year: 'numeric',
    month: 'long',
    day: 'numeric',
    weekday: 'long',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit',
    hour12: false
  });
  return formatter.format(new Date());
}

// 实时更新时钟
setInterval(() => {
  document.getElementById("digital-clock").innerHTML = getDateTime();
}, 1000);

HTML代码

<div id="digital-clock"></div>

这段代码会输出类似 2024年10月26日 星期六 15:45:30 的格式,完全匹配你的期望效果。

2. 支持非英语语言显示

Intl.DateTimeFormat 原生支持多语言本地化,只需修改第一个参数的语言代码即可切换:

  • 英文(美国):'en-US',输出示例:October 26, 2024 Saturday 15:47:12
  • 日文:'ja-JP',输出示例:2024年10月26日 土曜日 15:48:05
  • 法文:'fr-FR',输出示例:26 octobre 2024 samedi 15:49:20

替换对应语言代码后,就能自动生成符合目标语言习惯的日期时间格式。


内容的提问来源于stack exchange,提问作者Nurzam Faiz

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 17:05:19