Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 views
in Technique[技术] by (71.8m points)

javascript - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...