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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

trebbeCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ProculopsisCommented:

//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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JScript

From novice to tech pro — start learning today.