0

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;
    }
}
3
  • 2
    because your variable named Date (please change this) is type string[] because it's an array of strings Commented Apr 4, 2022 at 1:53
  • Hi @Samathingamajig but this does not resolve the issue...i knew it was a type string. thanks Commented Apr 4, 2022 at 2:17
  • 1
    Convert the strings into real date objects Commented Apr 4, 2022 at 3:04

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.