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
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;
}
)
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];
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
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'));
}
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.
/**
* 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>
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.