Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all months name from year in moment.js

I want to get all months name from year in moment.js

if the year is 2011, then i want to all months name in momentjs

i have tried this below code, but it's not working for me.

var xxx = moment().months(2011); 

Showing result is

enter image description here

also i have tried xxx.months(), but it's return result is 7

but i want jan,feb,mar,......dec. hmm What can i do?

like image 944
Ramesh Rajendran Avatar asked Mar 27 '15 14:03

Ramesh Rajendran


People also ask

How do you show month name in moments?

Instead of unix() use the format() function to format the datetime using the MMMM format specifier for the month name. Show activity on this post. You can simply use format('MMMM') .

How do you get month name from month number using moment?

To get month name from two digit month number with JavaScript, we can use the moment. js' format method. We call format with 'MMMM' to get the full month name. As a result, formattedMonth is 'October' since we called month with 9 before calling format .

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.


2 Answers

There happens to be a function for that:

moment.monthsShort() // ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"] 

Or the same using manual formatting:

Array.apply(0, Array(12)).map(function(_,i){return moment().month(i).format('MMM')}) 

I guess you want to display all names utilizing Moment.js locale data, which is a reasonable approach.

like image 65
Klaster_1 Avatar answered Oct 14 '22 16:10

Klaster_1


Using moment.js you have the following methods:

moment.months() // long names 

returns:

[ 'January',   'February',   'March',   'April',   'May',   'June',   'July',   'August',   'September',   'October',   'November',   'December' ] 

and

moment.monthsShort() // short names 

returns:

["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] 
like image 35
Mikel Sanchez Avatar answered Oct 14 '22 17:10

Mikel Sanchez