使用 Moment js 从一年中获取所有月份并从一个月中获取所有周的问题

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

我是 moment.js

的新人

我想要 3 种类型的输出

我如何使用 moment.js 执行此操作?

我已经尝试了整个月

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

但我只有最后两位数字的年份。

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

我也看了文档,他们说

moment().months()接受0到11之间的数字,如果超出范围,会冒泡到年份。

当我使用 get days and weeks 时也出现了这个问题

如何解决 moment.js 中的这个问题?

使用日期作为参数 (12-12-2011) 查找年、月、月、星期和星期几的数值,如果您需要单词 (Monday, December) 值,只需使用格式 moment.js 和翻译。

你用 moment().months(2011) - moment() return 日期时间现在做的有点不正确,months(value) add value months to your moment 检查并查看:

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

现在阅读一些关于上周的文章 here (52/53) 之间的变化。

现在是您问题的解决方案,

得到一年中的所有月份,老兄认真(12个月)无论如何:

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

获取一年中的周数而不是月中的周数(使用月中的周数你会感到困惑)

 moment("12-26-2011", "MM-DD-YYYY").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:

星期几:

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

希望对您有所帮助。