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 ?
format('YYYY-MM-DD'); Calling moment() gives us the current date and time, while format() converts it to the specified format. This example formats a date as a four-digit year, followed by a hyphen, followed by a two-digit month, another hyphen, and a two-digit day. );
js parsing date and time. We can parse a string representation of date and time by passing the date and time format to the moment function. const moment = require('moment'); let day = "03/04/2008"; let parsed = moment(day, "DD/MM/YYYY"); console. log(parsed.
var check = moment('date/utc format'); day = check. format('dddd') // => ('Monday' , 'Tuesday' ----) month = check. format('MMMM') // => ('January','February.....) year = check. format('YYYY') // => ('2012','2013' ...)
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()
Or you can convert the string to date then extract the year:
var date = new Date(dateString); if (!isNaN(date)) { return d.getFullYear(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With