JavaScript Code Issues

I have the following code in my PHP page that allows me to input a date in the sent_date field & it calculates the other fields automatically. The issue I'm having is, if one of the calculated dates fall on a Saturday or Sunday, I need the date field to display the previous Friday's date....

Can someone help me adjust this code to do that?
<script type="text/javascript">
var valid;

function d2(v) { return (v<10)?("0"+v):v; }

function dcheck(form) {
var s = form.sent_date.value;
var dr = form.due_rev.value;
var su = form.due_suspo;
var clk = form.due_clerk;
var att = form.due_attny;
var own = form.due_owner;
var sent = new Date(s);
var due_rev = new Date(dr);
var due_suspo = new Date(su);
var due_clerk = new Date(clk);
var due_attny = new Date(att);
var due_owner = new Date(own);

if (isNaN(due_rev)) {
due_rev = new Date(due_suspo.getFullYear(),due_suspo.getMonth(),due_suspo.getDate()-2);
}
if (isNaN(due_suspo)) {
due_suspo = new Date(due_clerk.getFullYear(),due_clerk.getMonth(),due_clerk.getDate()-2);
}
if (isNaN(due_clerk)) {
due_clerk = new Date(due_attny.getFullYear(),due_attny.getMonth(),due_attny.getDate()-2);
}
if (isNaN(due_attny)) {
due_attny = new Date(sent.getFullYear(),sent.getMonth(),sent.getDate()-36);
}
if (isNaN(due_owner)) {
due_owner = new Date(sent.getFullYear(),sent.getMonth(),sent.getDate()-7);
}


form.sent_date.value = (sent.getFullYear()) + "-" + d2(sent.getMonth()+1) + "-" + d2(sent.getDate());
form.due_rev.value = (due_attny.getFullYear()) + "-" + d2(due_attny.getMonth()+1) + "-" + d2(due_attny.getDate()-6);
form.due_suspo.value = (due_attny.getFullYear()) + "-" + d2(due_attny.getMonth()+1) + "-" + d2(due_attny.getDate()-4);
form.due_clerk.value = (due_attny.getFullYear()) + "-" + d2(due_attny.getMonth()+1) + "-" + d2(due_attny.getDate()-2);
form.due_attny.value = d2(due_attny.getFullYear()) + "-" + d2(due_attny.getMonth()+1) + "-" + d2(due_attny.getDate());
form.due_owner.value = (due_owner.getFullYear()) + "-" + d2(due_owner.getMonth()+1) + "-" + d2(due_owner.getDate());
return true;
}

</script>

Open in new window

LVL 15
wantabe2Asked:
Who is Participating?
 
trebbeConnect With a Mentor Commented:
Something along the lines of this....

Set the date, then action a while loop, if the day is == to 0 (Sunday) or 6 (Saturday) then remove 1 day from the date and check again, after a couple of loops you will end up on a friday (5).

var d = new Date();

while (d.getDay() == 6 || 0) {
    //alert(d.getDay());
    d.setDate(d.getDate() - 1);
}

Open in new window

0
 
ProculopsisConnect With a Mentor Commented:

//try this function:

function previousWeekday( dueDate ) {
  var result = dueDate;
  if ( dueDate.getDay() > 4 ) {
    result.setDate( result.getDate() - dueDate.getDay() + 4 );
  }
  return result;
}
0
 
trebbeCommented:
var d = new Date();

while (d.getDay() != 6 && d.getDay() != 0) {
    //alert(d.getDay());
    d.setDate(d.getDate() - 1);
}

 

Open in new window


Here is the wokring code i just tested
0
 
ProculopsisCommented:

//correction:

function previousWeekday( dueDate ) {
  var offset = 0;
  if ( dueDate.getDay() == 6 ) offset++;
  if ( dueDate.getDay() == 0 ) offset = 2;
  return new Date( dueDate.setDate( dueDate.getDate() - offset ) );
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.