I'm working on my employer's internal web presence and there's an old piece of VBScript that they want to save. It displays in IE but doesn't work in any other browsers because, well, VBScript from what I understand is deprecated. This script takes the current date and calculates what "unit" day it is on a Fire Department unit day calendar. Today (February 26th) for instance is "1 Unit" or "Green Unit" with the next in line being 2 Unit/Blue Unit and then 3 Unit/Red Unit. If it's a leap year then the extra day is considered "0 Unit" or "White Unit".
So here is the old VBScript:
Sub UpdateClock()
Dim iDOW, iElapsed, iLeapYears, iUnit, sDate, sTime, aDOW, aUnitColor, sHTML
aUnitCOlor = Array("white", "green", "blue", "red")
aDOW = Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
iElapsed = DateDiff("d", "3/10/2008", Date)
iDOW = WeekDay(Date)
iLeapYears = Int((Year(Date)-2008)/4)
If Month(Date) > 2 Then iLeapYears = iLeapYears
iElapsed = iElapsed + iLeapYears
iUnit = (iElapsed Mod 3) + 1
' sDate = aDOW(iDOW) & " " & FormatDateTime(Date,2)
sDate = WeekdayName(DatePart("w", Now())) & " " & FormatDateTime(Date, 2)
sTime = FormatDateTime(Time, 3)
sHTML = "<font size='1' face='Franklin Gothic Book'>" & sDate & "; " & sTime & "<br>"
sHTML = sHTML & "<font size='1' face='Franklin Gothic Book' color='" & aUnitColor(iUnit) & "'>" & CStr(iUnit) & " Unit</font><br>"
Clock.innerHTML = sHTML
End Sub
setInterval "UpdateClock()", 1000
And here is the JavaScript I've written to try to recreate it:
// This script is intended to calculate the fire department "unit" day
var d, sd, pd, psd, iElapsed, iLeapYears, iUnit
// Sets start date
sd = new Date("March 10, 2008");
// Sets current date
d = new Date();
// Calculates the milliseconds between each date and January 1, 1970
psd = sd.getTime();
pd = d.getTime();
// Calculates milliseconds between the two dates and divides by the number of milliseconds in a day to determine how many days have passed
iElapsed = (pd - psd) / 86400000;
// Calculates leap year
if (d.getMonth() > 1) {
iLeapYears = (d.getFullYear() - 2008) / 4;
} else {
iLeapYears = 0;
}
iElapsed = iElapsed + iLeapYears;
iUnit = (iElapsed % 3 + 1);
document.getElementById("DisplayUnitDay").innerHTML = iUnit;
For the current date it gives me a result of 3.9499762037034998 (based on my last refresh) but it should be telling me the unit day is 1 and not 3. 3 Unit is two days from now. I feel like I'm either missing something simple or the VBScript I'm working from is flawed (or maybe I'm not interpreting it correctly).
Can anyone point me in a direction? I mean my employer requires IE for all internal computers anyway but, personally, I'd like for it to work on other browsers as well.