Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the month number by month name with Moment.js

Tags:

momentjs

I am trying to return the month number passing the month name using MomentJS. For example if I pass "July" to moment() I would expect 7 to be returned.

After reading through the docs I tried several different ways, and this way came close...

console.log(moment().month("July")); 

In the console, buried in the response I could see this...

_monthsParse: Array[7] 

Could anyone please tell me how to return the month number using MomentJS correctly?

like image 915
Jose the hose Avatar asked Dec 11 '15 10:12

Jose the hose


People also ask

How do you find the month number from a moment?

log(moment(). month("July"));

What is Moment () hour ()?

The moment(). hour() Method is used to get the hours from the current time or to set the hours. Syntax: moment().hour(); or. moment().

What is Moment () in JavaScript?

Moment JS allows displaying of date as per localization and in human readable format. You can use MomentJS inside a browser using the script method. It is also available with Node. js and can be installed using npm.

How do you find the beginning of the month in a moment?

Use the Moment. We call moment to create a Moment object with the current date and time. Then we call clone to clone that object. Then we call startOf with 'month' to return the first day of the current month. And then we call format to format the date into the human-readable YYYY-MM-DD format.


1 Answers

Try :

moment().month("July").format("M"); 

Relevant documentation: http://momentjs.com/docs/#/get-set/month/

alert(moment().month("July").format("M"));
<script src="https://momentjs.com/downloads/moment.min.js"></script>
like image 50
kometen Avatar answered Sep 23 '22 02:09

kometen