In the following code, I like to reformat date, the first element in each inner array to get them like in the output with Google Apps Script. How can I accomplish that? Thank you!
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]];
}
CodePudding user response:
To convert the date to the desired form, you can use the .toLocaleDateString("en-US", options)
method
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]];
}