How to find the day, month and year with moment.js

JavascriptMomentjs

Javascript Problem Overview


2014-07-28

How do I find the month, year and day with moment.js given the date format above?

var check = moment(n.entry.date_entered).format("YYYY/MM/DD");
var month = check.getUTCMonth();
var day = check.entry.date_entered.getUTCDate();
var year = check.entry.date_entered.getUTCFullYear();



                

Javascript Solutions


Solution 1 - Javascript

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

Solution 2 - Javascript

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

Solution 3 - Javascript

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' ...)  
 

Solution 4 - Javascript

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

Solution 5 - Javascript

Here's an example that you could use :

 var myDateVariable= moment("01/01/2019").format("dddd Do MMMM YYYY")
  • dddd : Full day Name

  • Do : day of the Month

  • MMMM : Full Month name

  • YYYY : 4 digits Year

For more informations :

https://flaviocopes.com/momentjs/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionviniView Question on Stackoverflow
Solution 1 - JavascripthszView Answer on Stackoverflow
Solution 2 - JavascriptChris SchmitzView Answer on Stackoverflow
Solution 3 - JavascriptSai RamView Answer on Stackoverflow
Solution 4 - JavascriptVikasdeep SinghView Answer on Stackoverflow
Solution 5 - JavascriptSaad JoudiView Answer on Stackoverflow