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

如何在Google Apps Script中将RFC 3339日期转换为指定格式

解决Google Apps Script中RFC3339日期转指定格式的问题

直接通过两步就能实现需求:

  1. 将RFC3339字符串转为Date对象
    Google Apps Script的Date构造函数原生支持解析RFC3339格式的字符串,无需额外处理:
const rfc3339Str = "2022-11-03T21:30:41.043Z";
const dateObj = new Date(rfc3339Str);
  1. 用Utilities.formatDate格式化日期
    把转换后的Date对象传入方法,指定目标时区和格式即可:
// UTC时区下转成yyyy-MM-dd HH:mm格式
const formattedDate = Utilities.formatDate(dateObj, "UTC", "yyyy-MM-dd HH:mm");
console.log(formattedDate); // 输出:2022-11-03 21:30

// 如果需要本地时区,比如上海时区,替换时区参数
const shanghaiFormatted = Utilities.formatDate(dateObj, "Asia/Shanghai", "yyyy-MM-dd HH:mm");
console.log(shanghaiFormatted); // 输出:2022-11-04 05:30

补充说明

  • 时区参数需使用IANA时区标识符(如"Asia/Shanghai"、"Europe/London"),可根据实际需求调整。
  • 该方法依赖V8引擎对ISO 8601格式(RFC3339属于其子集)的原生支持,在Google Apps Script环境中稳定可靠。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 11:55:33