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

Google Sheets App Script下API时间戳转二维日期数组问题咨询

代码调整方案

你只需提前初始化空二维数组存储转换结果,循环时将每次生成的日期字符串包装为单元素子数组存入即可,调整后的代码如下:

const timestamps = [[1.6370611672429312E18], [1.63706107263277082E18]];
// 初始化空数组存储最终转换结果
const convertedDates = [];
for (let i = 0; i < timestamps.length; i++) {
  const timestamp = new Date(timestamps[i][0] / 1000000);
  const convertDate = Utilities.formatDate(timestamp, "GMT", "MM-dd-yyyy HH:mm:ss");
  // 把转换后的日期包装为单元素子数组,推入结果数组
  convertedDates.push([convertDate]);
}

// 可以直接调用setValues批量写入
sheet.getRange(1,1,convertedDates.length, convertedDates[0].length).setValues(convertedDates);

可选优化

如果你不需要固定格式的字符串,希望表格能原生识别日期类型用于后续计算,可以去掉Utilities.formatDate步骤,直接存入Date对象,写入后可在表格界面自行调整日期展示格式:

const convertedDates = timestamps.map(item => [new Date(item[0]/1000000)]);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 08:06:05