Keep getting error Argument of type 'string[]' is not assignable to parameter of type 'Date[]'. Type 'string' is not assignable to type 'Date'.
const Date: string[] = ['April 1, 2017 10:39 PM', 'April 1, 2017 10:39 PM', 'January 1, 2018 9:39 PM', 'February 11, 2019 9:39 PM', 'April 1, 2019 10:39 PM']
const sDates = Utils.sDates(Date, false)
console.log(sDates)
Function to SortDate
export namespace Utils {
export function sDates(items: Date[], isDescending?: boolean): Date[] {
let sDateArr: Date[] = [...items];
if (isDescending) {
sDateArr.sort(function (dateA: Date, dateB: Date) { return dateB - dateA });
} else {
sDateArr.sort(function (dateA: Date, dateB: Date) { return dateA - dateB});
}
return sDateArr;
}
}
CodePudding user response:
Convert all the dates from you array to Date objects
// rename your array to avoid conflict with the native Date object
const dates: string[] = ['April 1, 2017 10:39 PM', 'April 1, 2017 10:39 PM', 'January 1, 2018 9:39 PM', 'February 11, 2019 9:39 PM', 'April 1, 2019 10:39 PM']
const sDates = Utils.sDates(
dates.map((date) => new Date(date)),
false
);
console.log(sDates)