我有一个混合日期、文本和空白值的初始数组。我的目标是获得一个仅包含日期的数组。我已经尝试过,但它不起作用,没有任何值被识别为日期......你能帮我吗?
function fil () {
var initialArray = [ '10 April 2024',
'{AS} BG AV (5€, Return of Capital)',
'{SW} FABG SS (0.45sek, Regular Cash), INDT SS (2.85sek, Regular Cash)',
'',
'11 April 2024',
'{DE} ROCKB DC (43dkk, Regular Cash)' ];
//Today's date
var d = new Date();
var day = d.getDate();
var month = d.getMonth();
var year = d.getFullYear();
var Today = new Date(year,month,day,0,0,0);
var Tomorrow = new Date(Today.getFullYear(),Today.getMonth(),(Today.getDate()+1))
const filteredArray = initialArray.map(d=>Tomorrow - d).filter(v=>v>0);
const minIndex = filteredArray.indexOf(Math.min(...filteredArray))+1;
const minDate = filteredArray[minIndex];
console.log(new Date (filteredArray)); // Invalid Date in the console
console.log(minDate); // undefined in the console
console.log(Utilities.formatDate(new Date(minDate),"GMT+2","dd-MM-yyyy")); // 01-01-1970 in the console
}
这会将数组过滤为仅能在 JavaScript 中成功解析为日期的字符串。这是有效的,因为任何无法解析的内容都会返回 NaN,这是一个虚假值:
let onlyDates = (arr) => {
return arr.filter(d => Date.parse(d))
}
let initialArray = ['10 April 2024',
'{AS} BG AV (5€, Return of Capital)',
'{SW} FABG SS (0.45sek, Regular Cash), INDT SS (2.85sek, Regular Cash)',
'',
'11 April 2024',
'{DE} ROCKB DC (43dkk, Regular Cash)'
];
console.log(onlyDates(initialArray))