0

I have a datepicker with ID "dtpicker".

I need to make sure that user will get an alert popup if selected date is within five working days from current day(today).

I have a almost working solution:

function DateRule()
{
  var dt = new Date(); 
  dt.setDate(dt.getDate() + 5);
  var date = dt.toISOString().substring(0, 10);
  userdatepick = NWF$("#" + datepicker).val();

  if (userdatepick < date)
  {
  alert("Reminder: you have selected a startdate that is earlier then five workingdays from todays date")
  }
}

I tested this with todays date and it works almost as it suppose to when I select dates throught datepicker between 2010-10-18 - 2010-10-22 I get the alert reminder which is correct. When I select 2010-10-23 I dont get reminder alert, but it should since its weekend day and not working day. 2010-10-25 should not give reminder thought.

Could a solution be to ignore weekend days when adding five days to current date(today). For an example when I add five days to my variable It jumps over weekend days?

Any help or tips is appreciated

Obsivus
  • 8,231
  • 13
  • 52
  • 97
  • 1
    Take a look at http://momentjs.com/. Makes working with dates so much easier. – mbx-mbx Oct 18 '16 at 13:23
  • 1
    Possible duplicate: http://stackoverflow.com/q/6499944/1493235 – JohannesB Oct 18 '16 at 13:26
  • @JohannesB thats a 5 years old question – Obsivus Oct 18 '16 at 13:31
  • This is really a duplicate of [*many questions*](http://stackoverflow.com/search?q=%5Bjavascript%5D+add+business+days) that have been asked before about adding or subtracting business days from a date. – RobG Oct 19 '16 at 02:07
  • An old question is not necessarily a bad or non-relevant question @Obsivus , people struggle with the same programming problems every day. – JohannesB Oct 19 '16 at 14:06

1 Answers1

1

You can validate something like the below one.

function validateDate(txtDate) {

  var selectedDate = new Date(txtDate),
    date = new Date(),
    days = 5;

  while (days > 0 && (date = new Date(date)) < selectedDate) {
    date = date.setDate(date.getDate() + 1);
    if (!isWeekend(date)) {
      days -= 1;
    }
  }

  if (days !== 0) {
    console.log("Reminder: you have selected a startdate that is earlier then five workingdays from todays date");
  }

}

var isWeekend = function(date) {
  var dt = new Date(date);

  if (dt.getDay() == 6 || dt.getDay() == 0 /* check here for holidays */) {
    return true;
  }

  return false;
}

validateDate("Oct 23, 2016");
Thaadikkaaran
  • 5,088
  • 7
  • 37
  • 59
  • Note that the *setDate* method returns the time value of the updated date, so `date = date.setDate(date.getDate() + 1);` sets *date* to a number, not a Date object. – RobG Oct 19 '16 at 02:06
  • @RobG Yes, But in `isWeekend` function, we are constructing date object again – Thaadikkaaran Oct 19 '16 at 06:01
  • "We"? Why not just delete the `date =` part and keep it as a Date? Then there is no need to create a new Date in *isWeekend*. ;-) – RobG Oct 20 '16 at 03:26
  • if you remove `date = ` part, then `date.setDate(date.getDate() + 1)` will throw error since `date` is a number (Time). – Thaadikkaaran Oct 20 '16 at 06:06
  • No, it will not. The right hand side is evaluated first, while *date* is still a Date. The method returns a number that you then assign to *date* so that its value becomes a number, not a Date. – RobG Oct 21 '16 at 00:32
  • Yes, it is. Did you tried the above code without `date =` part?. I tried, throws `date.getDate is not a function` error. Because first time, as you told it still Date but for second iteration, it becomes number. – Thaadikkaaran Oct 21 '16 at 06:03
  • I don't know what code you are running, but changing `date = date.setDate(date.getDate() + 1);` to `date.setDate(date.getDate() + 1);` should not and does not throw an error. Please post the code that does. – RobG Oct 21 '16 at 22:44