0

how to get a week start date from week number dynamically using moment js

I have the start date that can be a new date or future date and I have the week count dynamically, a form that dates I need to find the weeks start date. For example, if I have 21 days that means three weeks, I need to find each week start date. I tried with some bit of code.

weeklyFun = (value, date) => {
   const totaldays = (value * 7);
   const endDate = moment(date, "DD-MM-YYYY").add(totaldays, 'd').format("DD-MM-YYYY");
}

this.weeklyFun(3, '18-10-2019');

my expected output is for 21 days 18-10-2019, 25-10-2019, 01-11-2019

techie18
  • 774
  • 1
  • 7
  • 19

1 Answers1

0

Moment has a .day() function which return the number that day is from that week moment docs .day()

const date = moment("2019-10-21");
const dow = date.day();

In your case you can make a function which checks is current date is equal to 1:

const isFirstDayOfTheWeek= date => moment(date).day() === 1

console.log(isFirstDayOfTheWeek("2019-10-21")) // true
console.log(isFirstDayOfTheWeek("2019-10-22")) // false
VladNeacsu
  • 1,237
  • 19
  • 33