Question

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 ?

Était-ce utile?

La solution

You can use

moment("25/04/2012","DD/MM/YYYY").format("YYYY")
or    
moment("25/04/2012","DD/MM/YYYY").year()

in your example:

moment(item.date,"DD/MM/YYYY").year()

Autres conseils

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

var date = new Date(dateString);
if (!isNaN(date)) {
 return d.getFullYear();
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top