Mauro74 Mauro74 - 2 months ago 9
Javascript Question

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 ?

Answer

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()