Skip to content
Advertisement

date range logic computation (min current date)

Currently I have a mat date picker range. The logic is the min date on the calendar that the user can select is + 2 full days but exlude the counting of weekends for example

If today is 25(July 25, 2022 ) then the current date([min]=”currentDate”) should be 28 which is thursday July 28, 2022.

If today is 26(July 26, 2022 ) then the current date([min]=”currentDate”) should be 28 which is thursday July 29, 2022.

But if for example if its friday it should not include the count of weekends for example today is 29(July 29, 2022 ) then the current date([min]=”currentDate”)should be Wednesday 3(August 03, 2022 .

And same logic goes to every dates.

Any idea guys how would we make it possible with the calendar? Thanks.

#blitz

JavaScript

#html

JavaScript

#ts code

JavaScript

Advertisement

Answer

JavaScript

stackblitz

Update explained

The code create an array of 7 dates (each element in the array one day).

JavaScript

Transform the array [1,2,3,4,5..] in an array of dates, e.g. [2022-07-29,2022-07-30,2022-07-31,2022-08-01,2022-08-02…]

Then filter and only get the dates who are not sunday nor saturday.

JavaScript

the new array becomes like [2022-07-29,2022-08-01,2022-08-02,…]

Finally gets the element in position 2 (in the array the position 1 is the index 0, the position 2 is the index 1…)

JavaScript

gets 2022-08-01

If we need one day not sunday or saturday we can change the “i==1” by “i==2”.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement