user16357452
user16357452

Reputation:

Get year from array of dates

i have array of dates ["2003-05-13T19:00:00.000Z", "1998-02-03T20:00:00.000Z"] from which I want to get year from I am using getFullYear() but I get error that it is not a function, what am I doing wrong?

this.childEndYear = this.data.underageChildInfo?.map(i => (i.birthDate.getFullYear())); // ["2003-05-13T19:00:00.000Z", "1998-02-03T20:00:00.000Z"]
    console.log(this.childEndYear)

Upvotes: 0

Views: 707

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522396

You could just take a substring of the first 4 characters:

var input = ["2003-05-13T19:00:00.000Z", "1998-02-03T20:00:00.000Z"]
for (var i=0; i < input.length; ++i) {
    year = input[i].substring(0, 4);
    console.log(input[i] + ", year: " + year)
}

But parsing to a date is also an option. That would require that you know the format of the text dates. But, since you already know that, you could just use a string operation.

Upvotes: 1

Related Questions