I’ve posted MUCH cleaner code with just the two fields needed to solve my problem. If I type in a date of 04/16/2011 in the “sentence date” field, then click the calculate button, the date 03/05/2011 is displayed in the “Due to Reviewer” field. It subtracts 42 days from the date enetred into the “Sentence Date” field.
My problem is, sometimes when I type in a date in the “Sentence Date”, such as 04/16/2011, 42 days prior falls on a weekend & I need it to always be on a weekday. So with that said, is it possible to put some type on function in the code where if the date falls on a weekend (Saturday or Sunday), it will roll back & display Friday’s date?
Thanks again for the help!
<html>
<head>
<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 sent = new Date(s);
var dr = form.due_rev.value
var due_rev = new Date(dr);
if (isNaN(due_rev)) {
due_rev = new Date(sent.getFullYear(),sent.getMonth(),sent.getDate()-42);
}
I still can’t get it to work. Can someone insert the code in my code so I can test it? I have absolutley no experience in programming. I desparetley need assistance. This seems like an easy fix but I just don’t understand it. Thanks for any help given!
Here is a way to do it by subtracting days until you get to the Friday before the weekend. I have added a few extra boxes so that you can see what is going on.
[HIGHLIGHT=“”]
<!doctype HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
<html>
<head>
<title>Date adjustment</title>
<script type=“text/javascript”>
<!–
var gap=42, days=[“Sun”,“Mon”,“Tues”,“Wed”,“Thurs”,“Fri”,“Sat”];
//
function dcheck(form)
{ var sent = new Date(form.sent_date.value);
form.input_day.value =days[sent.getDay()];
var i, prevDate;
// take off days here
prevDate=new Date(sent.setDate(sent.getDate()-gap+1));
// check if weekend (day=0 or 6)
for(i=0;i<3;i++)
{ prevDate=new Date(prevDate.setDate(prevDate.getDate()-1));
if(prevDate.getDay()>0 && prevDate.getDay()<6){ break;}
}
//
// add result to form
form.due_rev.value = (prevDate.getFullYear()+0) + “-” + d2(prevDate.getMonth()+1) + “-” + d2(prevDate.getDate());
// show day of week
form.due_day.value =days[prevDate.getDay()];
// show days difference
var daysDiff=(new Date(form.sent_date.value).getTime()-new Date(form.due_rev.value).getTime());
form.days_diff.value=Math.ceil(daysDiff/(606024*1000));
}
// -------
function d2(v) { return (v<10)?(“0”+v):v; }
/
//–>
</script>
<style type=“text/css”>
<!–
p { margin-top:0px; margin-bottom:3px; }
–>
</style>
</head>