Biker John
Biker John

Reputation: 2711

Add one day to date - avoid weekends (saturdays, sundays)

I have a code that adds +1 day to the actual date.

var date = '30 Apr 2010';
var actual_date = new Date(date);
var final_date = new Date(actual_date.getFullYear(), actual_date.getMonth(), actual_date.getDate()+1);

What i would like achieve now is to avoid weekends (saturdays and sundays) when the actual date is friday.

Normal midweek example: 
tuesday, 5th -> wednesday, 6th

Weekend example:
friday, 6th -> monday, 9th 

Upvotes: 2

Views: 1729

Answers (8)

yunzen
yunzen

Reputation: 33439

var final_date = new Date(
    actual_date.getFullYear(), 
    actual_date.getMonth(), 
    actual_date.getDay() === 5 // Friday
      ? actual_date.getDate()+3 
      : actual_date.getDay() === 6 // Saturday 
      ? actual_date.getDate() + 2 
      : actual_date.getDate() + 1 // others
);

Upvotes: 0

presidentnickson
presidentnickson

Reputation: 1085

Use the getDay() function to check if its 5 (Friday). Then add 3 days on. See fiddle :http://jsfiddle.net/JLGxQ/

var date = '30 Apr 2010';
var actual_date = new Date(date);

if(actual_date.getDay() == 5){
     x = 3;   
} else {
     x = 1;   
}

var final_date = new Date(actual_date.getFullYear(), actual_date.getMonth(), actual_date.getDate()+x);

Upvotes: 0

Sani Huttunen
Sani Huttunen

Reputation: 24385

This should work:

var date = '30 Apr 2010';
var actual_date = new Date(date);

// Add 3 days if friday, 2 days if saturday otherwise add 1 day.
var daysToAdd = actual_date.getDay() === 5 ? 3 : actual_date.getDay() === 6 ? 2 : 1;

var final_date = new Date(actual_date.getFullYear(), actual_date.getMonth(), actual_date.getDate() + daysToAdd);

Or more verbose:

var daysToAdd = 1;
if (actual_date.getDay() === 5)
  daysToAdd = 3;
else if (actual_date.getDate() === 6)
  daysToAdd = 2;

Upvotes: 0

Matyas
Matyas

Reputation: 13702

The following example uses momentjs

var aDate = moment("22. Apr. 2011");  // your starting date
var DAY_IN_MSEC = 1000 * 60 * 60 * 24; // a day represented in milliseconds
var fiveDays = 5 * DAY_IN_MSEC; // five days


var newDate = moment(aDate + fiveDays); // add your 5 days to original date
var dayOfWeek = newDate.weekday(); // get the index of the day  *locale dependent
if (dayOfWeek >= 5) {             // is saturday, sunday *locale dependent
   var daysToMonday = 7 - dayOfWeek;
   newDate += daysToMonday * DAY_IN_MSEC; // add the difference needded until Monday
   newDate = moment(newDate); // transform the msecs in a moment object
}

alert('voila!');

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388316

Try

var date = '30 Apr 2010';
var actual_date = new Date(date);
var final_date = new Date(actual_date);
final_date.setDate(final_date.getDate() + 1 + (actual_date.getDay() > 4 ? 7 - actual_date.getDay() : 0));

Demo: Fiddle

Upvotes: 0

gog
gog

Reputation: 11347

You can add a day in a loop and check if the weekday is 0 (=Sun) or 6 (=Sat):

d = new Date(2014, 3, 4)   // Fri Apr 04

do {
   d.setDate(d.getDate() + 1)
} while(d.getDay() == 0 || d.getDay() == 6);

console.log(d); // Mon Apr 07

Upvotes: 3

hsz
hsz

Reputation: 152206

Just try with:

var date = '30 Apr 2010';
var final_date = actual_date = new Date(date);

do {
  final_date = new Date(final_date.getFullYear(), final_date.getMonth(), final_date.getDate()+1);
  var day = final_date.getDay();
} while (day == 6 || day == 0); // (6 is saturday, 0 is sunday)

Output:

Mon May 03 2010 00:00:00 GMT+0200 (CEST)

Upvotes: 0

Suor
Suor

Reputation: 3045

Test actual_date.getDay(), which is day of week. If it's 5 (friday) add 3 days, if it's 6 (saturday) add 2 days, add 1 day otherwise.

Upvotes: 2

Related Questions