-2

if I have an array of : ['Thursday', 'Friday']

and I want to generate 5 dates on the next dates of these days

for example, i want the result is the next Thursday is 14/7/2022, next Friday 15/7/2022 the output should be :

 Thursday  ,    Friday   ,   Thursday  ,   Friday    ,   Thursday   

=> output ['14/7/2022', '15/7/2022' , '21/7/2022' , '22/7/2022' , '28/7/2022']

2 Answers2

0

If you can use libraries for that, then moment.js will help you a lot.

From that question we can create a solution for your case.

You can use moment.js to get a date from your string, and then using solution from question above get the date of that day of week on next week

  • The problem is also, suppose that there are meeting sessions of 5 or more sessions. I want to distribute them on the specified days. Every Thursday and Friday there is a meeting session – Ahmed Tawil Jul 12 '22 at 13:52
0

This would be doable with two simple function in javascript

The first one would be to know the current date you are in ( & the day )

const d = new Date();
const day = d.getDay();
The getDay() method returns the day of the week (0 to 6) of a date.

Sunday = 0, Monday = 1, ... (See below):

More info here : https://www.w3schools.com/jsref/jsref_getday.asp.

Once you know the date, you would just need to convert your array from ['Thursday', 'Friday'] to [4,5]

Then you need to calculate the offset between the days

let offset = (targetDay - currentDayInNumber) % 7
if (offset < 0) { offset += 7 } // Want to make sure you offset is always positive

An example with tuesday = 2, thursday = 4, or monday = 1

let offset = (4 - 2) % 7 // ( 2 )
let offset = (1 - 2) % 7 // (-1)
offset = -1 + 7 // (6) we will be adding 6 days

Then you can simply loop and add days as you go

var date = new Date();
date.setDate(date.getDate() + days); // For tomorrow + 1 for example

Let me know if that helps, otherwise can provide you with the complete solution, but wanted to guide rather than give solution

-- Edit -- To complete this and to have the occurences, you could have a counter

const counterDays = {
 0:0,
 1:0,
 ..,
 6:0
}

When going through the loop, everytime you setup a day, you increase the counter for it This would be become something like this :

date.setDate(date.getDate() + offset + 7 * counterDays[targetDay]);
counterDays[targetDay]++;

I've provided you with the sample code here: https://onecompiler.com/javascript/3y9sb8dqe Hope this helps

Ziyed
  • 491
  • 2
  • 12
  • The problem is also, suppose that there are meeting sessions of 5 or more sessions. I want to distribute them on the specified days. Every Thursday and Friday there is a meeting session – Ahmed Tawil Jul 12 '22 at 13:52
  • You can easily adapt the above & add a multiplier for the number of occurence in the array [4,5,4,4], the first one would be 0x7, the second 4 would add 1x7, the third one would add 2x7, ... etc This isn't ideal, and in this case I would recommend the other solution to use moment.js – Ziyed Jul 12 '22 at 14:07
  • watch post update – Ahmed Tawil Jul 12 '22 at 16:16
  • I think from the above answer you should be able to adapt the example given here – Ziyed Jul 12 '22 at 18:25