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
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?
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') .
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 .
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.
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.
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"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With