【问题标题】:How to reformat dates in two dimensional arrays with Google Apps Script?如何使用 Google Apps 脚本重新格式化二维数组中的日期?
【发布时间】:2021-10-27 15:36:52
【问题描述】:

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

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]];
}

【问题讨论】:

    标签: google-apps-script date-format


    【解决方案1】:

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

    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]];
    }
    

    【讨论】:

    • 完美!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 2021-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多