我正在处理我的雇主的内部网络存在,并且存在他们想要保存的旧VBScript。它在IE中显示,但在任何其他浏览器中都不起作用,因为根据我的理解,VBScript已被弃用。该脚本获取当前日期并计算消防部门单位日历上的“单位”日。今天(2月26日)例如是“1单位”或“绿色单位”,下一行是2单位/蓝色单位,然后是3单位/红色单位。如果是闰年,那么额外的一天被认为是“0单位”或“白色单位”。将旧的VBScript转换为JavaScript
因此,这里是旧的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
,这里是JavaScript的我已经写了,试图重建它:
// 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;
当前日期它给我的结果3.9499762037034998(根据我上次刷新),但它应该告诉我单位日是1,而不是3. 3单位是从现在开始的两天。我觉得我要么缺少一些简单的东西,要么我正在从事的VBScript有缺陷(或者我没有正确解释它)。
任何人都可以指出我的方向吗?我的意思是,我的雇主无论如何都要求所有内部计算机都使用IE,但个人而言,我希望它能够在其他浏览器上运行。
请接受我最诚挚的道歉没有得到回本更快。在Stack Overflow上提出一个问题,然后在有人回答后放弃它一个月,这不是我想要做的第一印象。我在项目的另一部分受到了阻挠,我现在在搁置一段时间后重新审视这个问题。感谢您花时间为我提供一些帮助。我是GMT-4。今天应该是1或绿色单位的一天,但它注册为2 /蓝色。根据我的老板,每次闰年都需要调整。 – joshuaw
我使用错误的日期进行比较是非常可能的。以前的设计师有不同版本的VBScript分布在不同的页面上,这意味着成千上万行的意大利面条代码,这就是为什么我们在新网站上从头开始的原因。我刚接触JavaScript并不重要。 – joshuaw