-1

So I am kinda stuck in figuring out a certain aspect. What I want to do is the following:

Let's say I just have a simple date display, which will show a date such as October 10th, 2017 to an end user. And then there is an option to subtract a certain number of days from said date (an offset of 1, 2, 3, whatever offset is chosen).

What I am looking to do is completely exclude weekend dates from the count - so if today is Monday, October 9th, and an offset of 1 is selected, it goes to Friday the 6th; if an offset of 2 is chosen, it goes to Thursday the 5th; an offset of 3 goes to Wednesday the 4th...

If today was Wednesday, October 11th, an offset of 2 would take you to Monday the 9th, an offset of 4 would go to Thursday the 5th, and so on (completely disregards / skips weekend dates when counting / subtracting which day to land on).

I have so far been able to only find answers for the functionality to calculate the number of working days excluding weekends, and things of that nature (which I already have, using the momentjs-business npm module, but is not exactly what I need).

I did not post code because this is part of a much larger code base, and I feel posting snippets would only add to the confusion, since I believe the question is relatively simply and straightforward; I do not want to over complicate.

All I would like is to not include weekends at all when setting an offset from whichever date is displayed to the user (the date which is displayed to the user is from a database).

I hope this all made sense, and if more info is needed, please let me know. Thanks in advance for anyone that can point me in the right direction!

Lushmoney
  • 412
  • 11
  • 26
  • I don't get why someone downvoted - I did my research, and there was no answer for the specific question I posted, which is why I posted it. Don't understand why a downvote was needed, but if I did not look hard enough, someone please point it out, thanks... – Lushmoney Oct 10 '17 at 04:12
  • will the offset always subtract days? – kemotoe Oct 10 '17 at 04:17
  • Yes, whatever offset is selected, that number of days will always be subtracted (*unless of course it is 0, but no one would select a 0 day offset...*) – Lushmoney Oct 10 '17 at 04:18

3 Answers3

1

This will achieve what you want I think. Please note this is terribly inefficient. If your offset is very large it generates a new date every iteration of the loop. With some tinkering it could be optimized

let startDate = new Date('10/10/2017');
let endDate = "", offset = 2;
while(offset > 0){
    endDate = new Date(startDate.setDate(startDate.getDate() - 1));
    if(endDate.getDay() !== 0 && endDate.getDay() !== 6){
       offset--;
    }
}

Here is a working Fiddle

kemotoe
  • 1,730
  • 13
  • 27
  • Yep, definitely inefficient, but exactly what I was looking for!! The user offset will never be more than 5 for my particular use case. Thank you very much!! – Lushmoney Oct 10 '17 at 12:34
1

You can use moment-business library. It has the subtractWeekDays that:

Subtract week days from the moment, modifying the original moment. Returns the moment.

Your code could be like the following:

var m = moment("October 10th, 2017", "MMMM Do, YYYY");
business.subtractWeekDays(m, 2);

If you don't want to add an external library, have a look at addWeekDays and subtractWeekDays code.

VincenzoC
  • 30,117
  • 12
  • 90
  • 112
0

JavaScript date objects have a getDay() method that tells you what day of the week it is. You could use this to figure out which dates are weekends and exclude them.

var date = new Date();
var dayOfWeek = date.getDay();
console.log(dayOfWeek) // 1 for Monday, 2 for Tuesday, etc.
psgivens
  • 66
  • 2
  • Yes of course - that was the first thing I did, with native JS and with moment.js, and it does exclude them in finding out which day is a weekend and which is not, but it does not work with the way I intend it to. It works only if the offset selected ends up landing on one of those days (Saturday or Sunday), but if we are on Wednesday the 11th for example, & offset of 5 is selected, it will go to Friday the 6th (since when it counts backwards 5 days, it never lands on a weekend day), but what I need is for it to not even consider the weekends as part of the count, & end up on Wednesday the 4th – Lushmoney Oct 10 '17 at 04:08