Forum Moderators: open
I had modified a different script that I found online earlier that will subtract days from the current date and display it how I want, however, it doesn't take BUSINESS DAYS into account. It also counts weekend days, which I don't want it to do.
The *formatted script* (that includes weekend days) I was using is:
<SCRIPT LANGUAGE="JavaScript">
var now = new Date();
now.setDate(now.getDate()-2);
var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var date = ((now.getDate()<10)? "0" : "")+ now.getDate();
daymonth = months[now.getMonth()]
wkday = date;
document.write("<p align=center><font face=Verdana size=1 color=blue>");
document.write(daymonth);
document.write("</font><br><font face=Verdana size=6 color=blue>");
document.write(wkday);
document.write("</font></p>");
</script>
The script that I found that will only count BUSINESS DAYS is this:
<SCRIPT LANGUAGE="JavaScript">
function getNextBusinessDay() {
return getDeliveryDateObj(-3);
}
function getDeliveryDateObj(businessDaysLeftForDelivery) {
var now = new Date();
var dayOfTheWeek = now.getDay();
var calendarDays = businessDaysLeftForDelivery;
var deliveryDay = dayOfTheWeek + businessDaysLeftForDelivery;
if (deliveryDay >= 6) {
businessDaysLeftForDelivery -= 6 - dayOfTheWeek; //deduct this-week days
calendarDays += 2; //count this coming weekend
deliveryWeeks = Math.floor(businessDaysLeftForDelivery / 5); //how many whole weeks?
calendarDays += deliveryWeeks * 2; //two days per weekend per week
}
now.setTime(now.getTime() + calendarDays * 24 * 60 * 60 * 1000);
return now;
}
</SCRIPT>
I would kind of like to find the easiest way to combine those two scripts - formatted like the first script, but that doesn't count weekend days, like the second script. Does this make sense?
Any help anyone could give me would be GREATLY appreciated - I've been banging my head against the keyboard about this one all day!
Thanks,
Rebecca
For some reason, though, it's still not skipping the weekend days (I've changed it to subtract 8 days now instead of 3 so I could test if it was counting weekend days or not - somehow I'm only ending up with it subtracting 8 days, not 8 *business* days). I'm not sure if I did something wrong or not - the "don't count weekend days" script I found was originally designed to display a date in the FUTURE, not the past, so now I'm not sure if I was correct in my original assumption that I could just change it to a negative number and it would work the same, but display a past date and not a future date... Ugh.
Here's what I have now:
<script language=JavaScript>
function getNextBusinessDay() {
return getDeliveryDateObj(-8);
}
function getDeliveryDateObj(businessDaysLeftForDelivery) {
var now = new Date();
var dayOfTheWeek = now.getDay();
var calendarDays = businessDaysLeftForDelivery;
var deliveryDay = dayOfTheWeek + businessDaysLeftForDelivery;
if (deliveryDay >= 6) {
businessDaysLeftForDelivery -= 6 - dayOfTheWeek; //deduct this-week days
calendarDays += 2; //count this coming weekend
deliveryWeeks = Math.floor(businessDaysLeftForDelivery / 5); //how many whole weeks?
calendarDays += deliveryWeeks * 2; //two days per weekend per week
}
now.setTime(now.getTime() + calendarDays * 24 * 60 * 60 * 1000);
return now;
}
var now = getNextBusinessDay();
var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var date = ((now.getDate()<10)? "0" : "")+ now.getDate();
daymonth = months[now.getMonth()]
wkday = date
document.write("<p align=center><b><font face=Verdana size=1 color=blue>");
document.write(daymonth);
document.write("</font><br><font face=Verdana size=6 color=blue>");
document.write(wkday);
document.write("</font></b></p>");
</script>
Running the script today, Feb 2, it's returning a result of January 25 (8 days ago) instead of January 23 (8 BUSINESS days ago). I think it's my -8 thing that's the problem, but I can't think of any other way to ask it to subtract days instead of add them, and I can't figure out why the script would work for future dates, but not for past, unless there was something else I should have changed that I didn't...
Just to put things in context, the final script will be a static feature of a page that will eventually display two dates for two different programs our agents receive calls on - one that subtracts 3 business days for the first program, and one that subtracts 8 business days for the second program - which will help them quickly determine if the issue they're dealing with needs to be escalated because it has exceeded its cycle time. This was the only way I could think to do this without me having to go in and manually update the dates every day. If anyone knows an easier way to do what I'm looking for, I'm open for suggestions!
Halfway there!
Rebecca
and a warm welcome to these forums. ;)
Take a look at this example, it may suit your requirements...
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>3 & 8 business days ago</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><style type="text/css">
#container {
width:600px;
padding:10px 0;
border:3px double #999;
margin:30px auto;
}
#today,#ago3,#ago8 {
line-height:30px;
text-align:center;
}
</style><script type="text/javascript">
window.onload=function(){
nowAndThen();
}
function nowAndThen() {days=new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
now=new Date();
yy=now.getUTCFullYear();
mm=now.getUTCMonth();
dt=now.getUTCDate();
dd=now.getUTCDay();
now=now.toLocaleDateString();
temp=dd;switch(dd) {
case 0:dd=3;dt3=dt-4;dt8=dt-11;
break;
case 1:dd=3;dt3=dt-5;dt8=dt-12;
break;case 2:dd=4;dt3=dt-5;dt8=dt-12;
break;case 3:dd=5;dt3=dt-5;dt8=dt-12;
break;case 4:dd=1;dt3=dt-3;dt8=dt-10;
break;case 5:dd=2;dt3=dt-3;dt8=dt-10
break;case 6:dd=3;dt3=dt-3;dt8=dt-10;
break;
}
backthree=new Date(yy,mm,dt3).toLocaleDateString();
backeight=new Date(yy,mm,dt8).toLocaleDateString();document.getElementById('today').firstChild.nodeValue='Today is '+days[temp]+' '+now;
document.getElementById('ago3').firstChild.nodeValue='Three business days ago it was '+days[dd]+' '+backthree;
document.getElementById('ago8').firstChild.nodeValue='Eight business days ago it was '+days[dd]+' '+backeight;
}
</script></head>
<body><div id="container">
<div id="today"> </div>
<div id="ago3"> </div>
<div id="ago8"> </div>
</div></body>
</html>