joshuaw
joshuaw

Reputation: 11

Converting an old VBScript to JavaScript

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.

Upvotes: 1

Views: 4097

Answers (1)

Robin Mackenzie
Robin Mackenzie

Reputation: 19319

There's a few factors to take into account in your re-write:

  • It is not clear whether sd is 10th March 2008 or 3rd October 2008 - in any case it seems they both give an answer of 1 which may be a lucky coincidence!

  • The VBScript says If Month(Date) > 2 Then but your javascript says if (d.getMonth() > 1) so that needs a correction

  • The VBScript has this line iLeapYears = Int((Year(Date)-2008)/4) - you should use Math.floor as the equivalent of VBScript int in this section: iLeapYears = (d.getFullYear() - 2008) / 4;

  • Use a modern library like moment.js to rid yourself of the heavy lifting around the date manipulation and formatting etc.

  • Did it ever work for white team? iUnit = (iElapsed Mod 3) + 1 is never going to be 0 for +ve iElapsed. VBScript can have 0- or 1-based arrays but there's no Option Base 1 there so have to assume that for aUnitCOlor = Array("white","green","blue","red") then aUnitCOlor(0) is white.

So 27/2/17 is a green day for maybe because my time zone is GMT+11. What time zone are you in?

// unit colors
var unitColor = ['white', 'green', 'blue', 'red'];

// elapsed days
var start = moment(new Date('March 10, 2008'));
var today = moment(new Date);
var elapsed = today.diff(start, 'days');

// update for leap year
if (today.format('M') > 2) {
  elapsed += Math.floor((today.format('Y') - 2008) / 4);
}

// get unit
unitIndex = (elapsed % 3) + 1;

// render
el = document.getElementById('out');
el.innerHTML = today.format('YYYY-MM-DD') + ' is a day for the ' + unitColor[unitIndex] + ' team';
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
<div id="out"></div>

Upvotes: 1

Related Questions