简体   繁体   中英

Isuue for get all months from a year and get all weeks from a month using Moment js

am a new for moment.js

i want 3 type of outputs

  • Get all months from a year

  • get all weeks from a month in Moment js

  • get all days from a week

How can i do this using moment.js?

I have tried for get all month

moment().months(2011);// it's working for my year is 2011

but i have only last 2 digits of year.

moment().months(11);// It's give wrong values or my year is 11

I also read the document, they said

moment().months() Accepts numbers from 0 to 11. If the range is exceeded, it will bubble up to the year.

This problem also accrued when i use get days and weeks

How can i solve this problem in moment.js?

Use a date as parameter (12-12-2011) find year month, month week, and week day numerical values, if you need words (Monday, December) values just use format of moment.js and translations.

A bit incorrect what you do with moment().months(2011) - moment() return date time now, months(value) add value months to your moment check and see:

moment().months(2011).format("LLLL"); //result Tuesday, August 27, 2182 ...

Now read a bit about year last week here variations between (52/53).

Now the solution for your problem,

get all months of the year, dude seriously (12 months) anyway:

 moment("12-26-2011", "MM-DD-YYYY").month() + 1; 

get weeks of the year not of the month (you will be confuzed using week of month)

 moment("12-26-2011", "MM-DD-YYYY").week();

or try this (month week):

var curr_month = moment("12-26-2011", "MM-DD-YYYY").month(); 
var prev_month_last_week = moment("01-01-2011", "MM-DD-YYYY").add(curr_month -1, "month").week();
var your_week_per_month = moment("12-26-2011", "MM-DD-YYYY").week() - prev_month_last_week; //from 1 to 4:

Day of the week:

moment("12-26-2011", "MM-DD-YYYY").day();

Hope it's helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM