简体   繁体   English

如何使用moment.js在节点js中生成周数数组?

[英]How to generate an array of week numbers in node js using moment.js?

I've use case in which I need to generate an array of last 6 week numbers starting from current week number.我有一个用例,我需要从当前周数开始生成一个最近 6 周数的数组。 Now this is 5th week of 2020. I have to create an array like,现在是 2020 年的第 5 周。我必须创建一个数组,例如,

[52,1,2,3,4,5]

This is a dynamic array which has to be generated using moment.js week() method.这是一个动态数组,必须使用 moment.js week() 方法生成。 I know how to get current week number using moment but don't know how to create this kind of an array.我知道如何使用 moment 获取当前周数,但不知道如何创建这种数组。

It's the 5th week of 2020 ;-).这是 2020 年的第 5 周 ;-)。 A simple for loop will do the job:一个简单的 for 循环将完成这项工作:

const dates = []
for (let i = 0; i < 6; i += 1) {
   dates.unshift(moment().add(-i, 'weeks').week())
}
console.log(dates);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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