javascriptdatedaysmit-scratch

How to get a JavaScript "days since 2000" that is identical to that of Scratch's block of the same name?


What do I need to do to make a "days since 2000" JS function that provides a value identical to that provided by the reporter in Scratch?: days since 2000

The Scratch Wiki page states that

It reports the number of days (and fractions of a day) since 00:00:00 1 January 2000 (UTC).

I've got some code working that tries to conform to that, but it isn't the exact same as what Scratch gives and it's driving me nuts. At a same time, my code returns 9427.659404375 , while Scratch returns 9427.867728310186 . My current code is:

dayssince2000=function(){   
return((new Date() - new Date(2000,0,1))/86400000)
}
console.log("Days since 2000: "+dayssince2000())

See, while this is considered correct, what I need is a function identical to what they have, and I'm struggling to get that. I want to get this done with zero ChatGPT, zero libraries, zero build steps or anything requiring a server. I'm also working with a limitation of not being able to access GitHub, due to school filters, which is why I can't just go on and view their source code to copy/paste the function into my code. Any help would be appreciated heavily :D


Solution

  • I'm also working with a limitation of not being able to access GitHub, due to school filters, which is why I can't just go on and view their source code to copy/paste the function into my code.

    I hate to ruin the suspense, but here's the function copy/pasted from GitHub:

    daysSince2000 () {
        const msPerDay = 24 * 60 * 60 * 1000;
        const start = new Date(2000, 0, 1); // Months are 0-indexed.
        const today = new Date();
        const dstAdjust = today.getTimezoneOffset() - start.getTimezoneOffset();
        let mSecsSinceStart = today.valueOf() - start.valueOf();
        mSecsSinceStart += ((today.getTimezoneOffset() - dstAdjust) * 60 * 1000);
        return mSecsSinceStart / msPerDay;
    }
    

    It doesn't seem worthwhile to try to reimplement this by trial and error when you can just use it right away (you can use a network outside your school to access GitHub).