首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何使用Google Apps脚本重新格式化二维数组中的日期?

如何使用Google Apps脚本重新格式化二维数组中的日期?
EN

Stack Overflow用户
提问于 2021-10-27 07:36:52
回答 1查看 32关注 0票数 0

在下面的代码中,我喜欢重新格式化date,即每个内部数组中的第一个元素,以获得它们,就像使用Google Apps脚本的输出一样。我如何才能做到这一点?谢谢!

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
function test() {
  const input = [['Fri Oct 15 2021 00:00:00 GMT-0400 (Eastern Daylight Time)', 123.19],
  ['Thu Oct 14 2021 00:00:00 GMT-0400 (Eastern Daylight Time)', 122.83]];

  // How should this code be changed to reformat the dates in each inner array to get the output like below
  var output = input.map(el => el);
  // output = [['Oct 15, 2021', 123.19],
  // ['Oct 14, 2021', 122.83]];
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-27 08:16:06

要将日期转换为所需的格式,可以使用.toLocaleDateString("en-US", options)方法

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
function test() {
  const input = [['Fri Oct 15 2021 00:00:00 GMT-0400 (Eastern Daylight Time)', 123.19],
  ['Thu Oct 14 2021 00:00:00 GMT-0400 (Eastern Daylight Time)', 122.83]];

  let options = { year: 'numeric', month: 'short', day: 'numeric' };

  // How should this code be changed to reformat the dates in each inner array to get the output like below
  var output = input.map(el => [(new Date(el[0])).toLocaleDateString("en-US", options),el[1]]);
  console.log(output)
  // output = [['Oct 15, 2021', 123.19],
  // ['Oct 14, 2021', 122.83]];
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69741571

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文