0
votes

I'm trying to make a macro that recognises Friday's date and adds 3 days to that date to skip the weekend and get the following Monday's date. I have a spreadsheet function that adds a new block for every day, updating to today's date. However, it doesn't take into account weekends so it would be great to skip these.

Apologies for the terrible rookie js code.

var date = new Date('July 26, 2019 13:00:00 -0500');

function skipWeekend(date){
while (date.getDate() <= 5 && date.getDate() >0){
    console.log("The day is...",date);
    date+=1;
} console.log("The day is...",date.getDate()+3);
return (date)
}
skipWeekend(date);

The day is... 24

Ideally want: The date is 'July 29, 2019 13:00:00 -0500'

Thanks for your help

3
There is no getDate in the code you've posted. You also have used and instead of && and are missing some parenthesis . Please create a minimal reproducible example with a runnable stack snippetadiga
Why recognise Friday's date? It's not a weekend, and adding two to it gives... Sunday? A weekend? Surely you'd want to recognise Saturday then add two days, or Sunday and add one?spender
'July 22, 2019 13:00:00 -0500'... A Monday? Maps to 'July 29, 2019 13:00:00 -0500'? That's a whole week and it wasn't even Friday. I'm struggling to make sense of your question.spender
Sorry my logic is trash. I have updated the code to reflect the 3 days needed to make Friday become a Mondayskiventist

3 Answers

1
votes

It has a very trivial solution, I hope this helps:

var date = new Date('July 27, 2019 13:00:00 -0500');
switch (date.getDay()){
        case 5:
            date = date.setDate(date.getDate() + 3);
            break;
        case 6:
            date = date.setDate(date.getDate() + 2);
            break;
        case 7:
            date = date.setDate(date.getDate() + 1);
            break;
        default:
            break
}
var nextWorkingDate = new Date(date)
0
votes

So you can do a little logic to determine the next weekday. Here is an example of how to achieve that

Date.prototype.addDays = function(days) {
    var date = new Date(this.valueOf());
    date.setDate(date.getDate() + days);
    return date;
}

function getNextWeekday(date){
  //If the day is sunday through Thursday, add 1 day
  if(date.getDay() >= 0 && date.getDay() < 5){
    return date.addDays(1);
  } else {
    //Otherwise, determine how many days to add
    return date.addDays(8 - date.getDay());
  }
}


var date = new Date('July 25, 2019 13:00:00 -0500');
document.write(date + ' -> ' + getNextWeekday(date) + '<br>');

date = new Date('July 26, 2019 13:00:00 -0500');
document.write(date + ' -> ' + getNextWeekday(date));
0
votes

If I understand correctly, you want a nextWorkingDay function. If so, that would be a better name for it.

One issue in your code is that you use the getDate method as if it is the getDay function, but the first gives the day of the month (1-based), the second, the day of the week (zero based).

You could just create a simple lookup-string. The following generates the next 30 working days:

function nextWorkingDay(date) {
    date.setDate(date.getDate() + +"1111132"[date.getDay()]);
}

var date = new Date('July 26, 2019 13:00:00');
for (let i = 0; i < 30; i++) {
    nextWorkingDay(date);
    console.log(date.toString());
}