'moment js extract year from string

I'm new to moment.js and I can't really understand the documentation. I'd like to manipulate some dates in string format.

I have my date as string from json feed and looks like:

var year = item.date
// it returns a string like "25/04/2012"

How do I extract the year from it using moment.js ?



Solution 1:[1]

Or you can convert the string to date then extract the year:

var date = new Date(dateString);
if (!isNaN(date)) {
    return date.getFullYear();
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Valor_