javascript - Generate an array of weeks since 2017 till now using moment.js - Stack Overflow

I need to list week array from 2017 till now. My week start from Monday and ends with Sunday I tried to

I need to list week array from 2017 till now. My week start from Monday and ends with Sunday I tried to do in moment.js,

const startDate = moment().isoWeekday('Monday').format('DD-MM-YYYY');
const endDate = moment().isoWeekday('Sunday').format('DD-MM-YYYY');

I tried to like this to get the week start date and end date but I don't know the process to next step.

Note my array list start with ["02-01-2017", "08-01-2017"] and ends with current week will be the Last one.

const result = [["02-01-2017", "08-01-2017"],...... ["05-01-2018", "11-01-2018"]]

I need to list week array from 2017 till now. My week start from Monday and ends with Sunday I tried to do in moment.js,

const startDate = moment().isoWeekday('Monday').format('DD-MM-YYYY');
const endDate = moment().isoWeekday('Sunday').format('DD-MM-YYYY');

I tried to like this to get the week start date and end date but I don't know the process to next step.

Note my array list start with ["02-01-2017", "08-01-2017"] and ends with current week will be the Last one.

const result = [["02-01-2017", "08-01-2017"],...... ["05-01-2018", "11-01-2018"]]
Share Improve this question edited Feb 6, 2018 at 7:37 CommunityBot 11 silver badge asked Feb 6, 2018 at 6:16 6round6round 1825 silver badges18 bronze badges 4
  • ["02-01-2017", "08-01-2017"] is this you input? – rijin Commented Feb 6, 2018 at 7:27
  • @rijin thank you reply no it not my input, I need to get the start week to date and end week date from 2017 till current week, i showed you the start date array of 2017 – 6round Commented Feb 6, 2018 at 7:31
  • so, inputs are 2017 and monday => returns all mondays till today from 2017? – rijin Commented Feb 6, 2018 at 7:33
  • @rijin my week start with Monday and end with Sunday. I need all the week start date and end date till current week this will be my result const result = {["02-01-2017", "08-01-2017"] .... ["05-01-2018", "11-01-2018"]} – 6round Commented Feb 6, 2018 at 7:37
Add a ment  | 

2 Answers 2

Reset to default 8

You can initialize your start date to 1st weekday of 1st January 2017 and iterate to all the dates less than today and keep adding days in an array.

var weeks = [];
var startDate = moment(new Date(2017,0,1)).isoWeekday(8);
if(startDate.date() == 8) {
    startDate = startDate.isoWeekday(-6)
}
var today = moment().isoWeekday('Sunday');
while(startDate.isBefore(today)) {
  let startDateWeek = startDate.isoWeekday('Monday').format('DD-MM-YYYY');
  let endDateWeek = startDate.isoWeekday('Sunday').format('DD-MM-YYYY');
  startDate.add(7,'days');
  weeks.push([startDateWeek,endDateWeek]);
}
console.log(weeks)
<script src="https://cdnjs.cloudflare./ajax/libs/moment.js/2.20.1/moment.min.js"></script>

Something like that would help?

let startDate = moment('2017-01-01').startOf('week').format('YYYY-MM-DD');
let endDate = moment(new Date()).startOf('week').format('YYYY-MM-DD');

const weeks = [];
while (startDate <= endDate) {
  weeks.push(startDate);
  startDate = moment(startDate).add(7, 'days').format('YYYY-MM-DD');
}

console.log('weeks:', weeks);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742302251a4418244.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信