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

为现有JavaScript日期代码添加UTC+1带秒时间并实现自动冬夏令时切换

修改后的JavaScript日期代码(支持UTC+1/夏令时自动切换、秒数显示)

直接用下面的代码替换你原来的脚本,它会自动处理UTC+1时区的冬夏令时切换,同时显示包含秒数的时间:

// 创建当前日期时间对象
const now = new Date();

// 指定时区:Europe/Berlin 对应冬令时UTC+1、夏令时UTC+2,自动切换
const targetTimeZone = 'Europe/Berlin';

// 日期格式配置:和原代码输出风格一致(星期全称、月份全称、日期、年份)
const dateFormatConfig = {
  weekday: 'long',
  year: 'numeric',
  month: 'long',
  day: 'numeric',
  timeZone: targetTimeZone
};

// 时间格式配置:24小时制,显示时、分、秒,带前导零
const timeFormatConfig = {
  hour12: false,
  hour: '2-digit',
  minute: '2-digit',
  second: '2-digit',
  timeZone: targetTimeZone
};

// 格式化日期和时间
const displayDate = now.toLocaleDateString('en-US', dateFormatConfig);
const displayTime = now.toLocaleTimeString('en-US', timeFormatConfig);

// 输出最终结果
document.write(`${displayDate}, ${displayTime}`);

关键说明:

  • 无需手动计算时区和夏令时:指定Europe/Berlin时区后,JavaScript会自动根据当前日期切换冬夏令时(冬令时用UTC+1,夏令时自动切换到UTC+2),比手动计算更准确可靠。
  • 时间显示包含秒数:通过timeFormatConfig里的second: '2-digit'配置,确保显示两位秒数(比如05而不是5)。
  • 保留原代码的日期风格:日期部分依然输出完整星期、月份名称,和你原来的代码显示逻辑一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 14:48:26