How to find the day, month and year with moment.js
Just try with:
var check = moment(n.entry.date_entered, 'YYYY/MM/DD');
var month = check.format('M');
var day = check.format('D');
var year = check.format('YYYY');
JSFiddle
I know this has already been answered, but I stumbled across this question and went down the path of using format
, which works, but it returns them as strings when I wanted integers.
I just realized that moment comes with date
, month
and year
methods that return the actual integers for each method.
moment().date()
moment().month() // jan=0, dec=11
moment().year()
If you are looking for answer in string values , try this
var check = moment('date/utc format');
day = check.format('dddd') // => ('Monday' , 'Tuesday' ----)
month = check.format('MMMM') // => ('January','February.....)
year = check.format('YYYY') // => ('2012','2013' ...)
I am getting day
, month
and year
using dedicated functions moment().date(), moment().month() and moment().year() of momentjs
.
let day = moment('2014-07-28', 'YYYY/MM/DD').date();
let month = 1 + moment('2014-07-28', 'YYYY/MM/DD').month();
let year = moment('2014-07-28', 'YYYY/MM/DD').year();
console.log(day);
console.log(month);
console.log(year);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>
I don't know why there are 48 upvotes for @Chris Schmitz answer which is not 100% correct.
Month is in form of array and starts from 0 so to get exact value we should use 1 + moment().month()