在以下代碼中,我喜歡重新格式化日期,即每個內部陣列中的第一個元素,以便在使用 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]];
}
uj5u.com熱心網友回復:
要將日期轉換為所需的形式,您可以使用該.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://www.uj5u.com/caozuo/339402.html
