Search code examples
javascripthtmlasp.net-mvcdate-arithmetic

Jscript returning different (Calculated) result on Dev and Test machine - using same code


Ok on My dev machine I have the following code:

JScript

setInterval(function () { myTimer(); }, 1000);

function myTimer() {
    var d = new Date();
    var hrs = d.getHours();
    var min = d.getMinutes();
    var sec = d.getSeconds();
    var dayOfWeek = d.getDay();

    // Let's do this logically.
    // To get the number of victims so far today we need to know how many seconds have passed so far today.
    var totalSeconds = sec + (min * 60) + (hrs * 3600); // Number of Seconds passed so far today.
    // Now multiply that by 14 in accordance with Symantec's research.
    var VictimsToday = totalSeconds * 14;

    // To get the number of victims this week we need to multiply
    // The number of seconds in a day by the number of full days and add the number of seconds of this partial day.
    var TotalSecondsInADay = 86400; // According to Google.
    var DaysSoFarThisWeek = dayOfWeek + 1; // Removes today as a partial day.
    // We already know how many seconds are in this partial day = totalSeconds
    // So our calculation is
    var tsD = TotalSecondsInADay * DaysSoFarThisWeek;
    var VictimsThisWeek = (totalSeconds + tsD) * 14;

    // Now we get the Day of the Year remove today as a partial day and calculate
    // Number of seconds a day by the number of complete days so far + todays partial day
    var DOY = DayOfYear();
    var tsY = DOY * totalSeconds;
    var VictimsThisYear = (totalSeconds + tsY) * 14;

    document.getElementById("FooterStatistics").innerHTML = Intl.NumberFormat().format(VictimsToday);
    document.getElementById("FooterStatistics2").innerHTML = Intl.NumberFormat().format(VictimsThisWeek);
    document.getElementById("FooterStatistics4").innerHTML = Intl.NumberFormat().format(VictimsThisYear);
}

function DayOfYear() {
    var today = new Date();
    var first = new Date(today.getFullYear(), 0, 1);
    var theDay = Math.round(((today - first) / 1000 / 60 / 60 / 24) + 0.5, 0);
    return theDay;
}

With the HTML being:

BODY

<div id="FooterStatistics"></div>
<div id="FooterStatistics2"></div>
<div id="FooterStatistics4"></div>

Now on the Dev machine the calculation returned in the DIV is:

Results

However, when I run this on W3Schools Tryit Editor: http://www.w3schools.com/js/tryit.asp?filename=tryjs_date_getday

I get a totally different calculated result:

TryIt results

Now I know the numbers will be different because of TimeZone differences - what should not be different is the actual calculations. The Dev machine's second result is smaller than the first which it should NEVER be, and the third result is apparently not even a number - where upon the results in the TryIt editor provide more reasonable asnwers.

How can this be?


Solution

  • While creating apps, keep using console.log() to check the values. This really helps to understand what is happening, in the future. This is not an alternative to debugging, though, but it helps. You could only remove them when your code is to go live, or you are sure it works and is well abstracted.

    PS: Avoid using w3schools.com. Use MDN. It's much better.