#google-apps-script #date-format
Вопрос:
В следующем коде я хотел бы переформатировать дату, первый элемент в каждом внутреннем массиве, чтобы получить их, как в выводе с помощью скрипта 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]];
}
Ответ №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]];
}
Комментарии:
1. Это прекрасно! Большое вам спасибо!