在下面的代码中,我喜欢重新格式化date,即每个内部数组中的第一个元素,以获得它们,就像使用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]];
}
发布于 2021-10-27 08:16:06
要将日期转换为所需的格式,可以使用.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]];
}
https://stackoverflow.com/questions/69741571
复制相似问题