如何在moment.js中获取当前月份的周数 [英] How to get week numbers of current month in moment.js

查看:155
本文介绍了如何在moment.js中获取当前月份的周数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取特定月份的星期数例如:2017年1月,该月的周数为[52,1,2,3,4,5]那么如何在数组中获得以上内容呢?谢谢

I want to get the week numbers of a particular month eg: January 2017 ,the weeknumbers in this months are [52,1,2,3,4,5] So how to get the above in an array? Thanks

推荐答案

如此高效:

 const firstDayOfMonth = moment(`${ year }-${ month }`, 'YYYY-MM-DD');
 let weekIndices = [];

 let currentDay = moment(firstDayOfMonth, 'YYYY-MM-DD');
 weekIndices.push(currentDay.isoWeek());

 while(currentDay.month() === firstDayOfMonth.month()) {
     currentDay.add(1, 'weeks');
     weekIndices.push(currentDay.isoWeek());
 }

return weekIndices;

这篇关于如何在moment.js中获取当前月份的周数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆