Ramesh Rajendran
Ramesh Rajendran

Reputation: 38683

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?

Upvotes: 36

Views: 90125

Answers (9)

msahin
msahin

Reputation: 1760

If you want to list month names in a select in React:

  <select
    value={value}
    onChange={handleChange}>

         {moment.months().map(item => (
            <option key={item}>{item}</option>
         ))}

  </select>

Upvotes: 2

Mikel Sanchez
Mikel Sanchez

Reputation: 2890

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"]

Upvotes: 55

RubbelDieKatz
RubbelDieKatz

Reputation: 1187

It's 2019 and you shouldn't be using moment.js anymore, since the vanilla Date() class has everything we need. Except for this little bit of functionality.

Here's what I use: There's the months NPM module which has all months in different languages. It's about 991 bytes gzipped.

Example with proper import syntax:

import months from 'months'

console.log(months.de)

Result:

[
  "Januar",
  "Februar",
  "März",
  "April",
  "Mai",
  "Juni",
  "Juli",
  "August",
  "September",
  "Oktober",
  "November",
  "Dezember"
]

Upvotes: 0

I needed an array with months number and months name to display them in ng-optiosn, so I extended a little bit Klaster_1's solution.

Array.apply(0, Array(12)).map(
                    function(_,i){
                          var elem    ={};
                          elem.id     = parseInt(i+1);
                          elem.name   = moment().month(i).format('MMM'); 
                          return elem;
                    }
              )

Upvotes: 1

Neha
Neha

Reputation: 673

You can use following function to get a list of Weekdays and Months for listing purpose -

var iIndex, 
sArrMonths, sMonth;
for(iIndex = 0; iIndex < 12; iIndex++)
{
    sMonth = moment.localeData().months(moment([0,iIndex]), "");
    sArrMonths.push(sMonth);
}

You can check live example here - Localized Month and Weekdays List

Upvotes: 3

Mr. Polywhirl
Mr. Polywhirl

Reputation: 48600

/**
 * Returns an array of all month names for a given language
 * in the specified format.
 *
 * @param lang {string} Language code
 * @param frmt {string} Possible values: {'M','MM','MMM','MMMM'}
 * @return the array of month names
 */
function getMonthNames(lang, frmt) {
  var userLang = moment.lang();            // Save user language
  moment.lang(lang);                       // Switch to specified language
  var months = [];                         // Months array
  var m = moment("2011");                  // Create a moment in 2011
  for (var i = 0; i < 12; i++)             // Loop from 0 to 12 (exclusive)
    months.push(m.months(i).format(frmt)); // Append the formatted month
  moment.lang(userLang);                   // Restore user language
  return months;                           // Return the array of months
}

function println(text) {
  text = arguments.length > 1 ? [].join.call(arguments, ' ') : text;
  document.getElementById('disp').innerHTML += text + '\n';
}

println('English:', getMonthNames('en-US', 'MMM'));
println('Bengali:', getMonthNames('bn', 'MMM'));
println('Español:', getMonthNames('es', 'MMM'));
#disp {
  white-space: pre;
  font-family: monospace;
}
<!-- http://cdnjs.com/libraries/moment.js/ -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment-with-locales.min.js"></script>

<div id="disp"></div>

Output

English: Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec
Bengali: জানু,ফেব,মার্চ,এপর,মে,জুন,জুল,অগ,সেপ্ট,অক্টো,নভ,ডিসেম্
Español: ene.,feb.,mar.,abr.,may.,jun.,jul.,ago.,sep.,oct.,nov.,dic.

Upvotes: 7

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.

Upvotes: 96

bvaughn
bvaughn

Reputation: 13487

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

Why does the year matter? Month names don't change.

You could get month names from Moment like so:

var m = moment();
for (var i = 0; i < 12; i++) {
 console.log(m.months(i).format('MMMM'));
}

Upvotes: 10

KJ Price
KJ Price

Reputation: 5964

You are going to have to access the months from an array:

var months = [ "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" ];

var monthInt = new Date().getMonth();
var monthString = months[monthInt];

Upvotes: 2

Related Questions