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

如何为VS Code用户自定义代码片段添加时区偏移功能

VS Code 自定义代码片段如何生成带自适应夏令时的时区时间戳

查阅VS Code官方用户自定义代码片段(User Defined Snippets)文档后确认,当前内置可用的时间/日期变量如下:

CURRENT_YEAR The current year
CURRENT_YEAR_SHORT The current year's last two digits
CURRENT_MONTH The month as two digits (example '02')
CURRENT_MONTH_NAME The full name of the month (example 'July')
CURRENT_MONTH_NAME_SHORT The short name of the month (example 'Jul')
CURRENT_DATE The day of the month as two digits (example '08')
CURRENT_DAY_NAME The name of day (example 'Monday')
CURRENT_DAY_NAME_SHORT The short name of the day (example 'Mon')
CURRENT_HOUR The current hour in 24-hour clock format
CURRENT_MINUTE The current minute as two digits
CURRENT_SECOND The current second as two digits
CURRENT_SECONDS_UNIX The number of seconds since the Unix epoch

上述变量未提供时区(TIMEZONE)或相对UTC的时区偏移(TIMEZONE OFFSET)相关能力,无法直接通过内置变量自动生成准确的带时区信息的时间戳。

现有内置变量拼接方案的缺陷

使用如下配置拼接ISO格式时间:

${CURRENT_YEAR}-${CURRENT_MONTH}-${CURRENT_DATE}T${CURRENT_HOUR}:${CURRENT_MINUTE}:${CURRENT_SECOND}

最终生成结果为:2022-06-03T07:32:09,该结果存在明显精度问题:缺少时区标识的时间戳与实际时间的偏差最高可达±23:59:59,无法反映对应时间的真实时区属性。

需求

在无需开发独立VS Code扩展的前提下,实现以下目标:

  • 生成格式为2022-06-03T07:32:09-07:00的人类可读时间戳,不使用Unix时间格式
  • 时区偏移可根据夏令时规则自动调整,例如夏令时结束后自动适配为2022-11-04T07:32:09-08:00格式

后续更新

该功能目前已正式落地支持,11月5日及之后创建新时间戳时,会自动显示-08:00而非-07:00,可自动适配夏令时切换,实用性很强。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.03 10:15:56