Date.js: Date.now() behaving oddly

2019-04-09 13:36发布

I'm using date.js.

The line time_container.innerHTML = Date.now().toString('T'); worked fine, briefly, and is now throwing errors in the Firebug console: radix must be an integer at least 2 and no greater than 36. It was certainly working earlier.

Note: The date.js toString() function uses special format specifiers.

var show_date = {
    setup: function() {
        setInterval(show_date.update, 5000);
    },
    update: function() {
        var date_container = app.get('js_date');
        var time_container = app.get('js_time');

        if (date_container) {
            date_container.innerHTML = Date.today().toString('dS of MMMM yyyy');
        }
        if (time_container) {
            //time_container.innerHTML = Date.now().toString('T');
            var d1 = new Date();
            time_container.innerHTML = d1.toString('T');
        }
    }
}
app.onload(show_date.setup);

app.get() is just a shortcut for document.getElementById(). app.onload() is (as you might guess) an onload function.

Commented out line is causing the problems. Replacement lines below the comment work, but don't give the format I want. T should output h:mm:ss tt (hours, minutes, seconds, am/pm). The am/pm bit is missing.

Also, I'm certain Date.now() was working earlier today. Perhaps I'll try playing with the computer clock to see whether that makes a difference.

Version of date.js included is date-en-IE.js. Claimed date in the code is 2008-05-13, even though I got it from the SVN checkout earlier today.

2条回答
做自己的国王
2楼-- · 2019-04-09 13:46

I had a same error in FireFox. By changing .toString() method to .toDateString() seems like took care of that problem. Example: Date.now().toDateString('M/d/yyyy HH:mm')

查看更多
来,给爷笑一个
3楼-- · 2019-04-09 14:07

ECMAScript 5 already has a Date.now() function that returns the number of milliseconds since January 1, 1970. You're apparently calling that version so the toString('T') call is on a number, not a Date object. Number.prototype.toString can only take a number from 2 to 36 as its argument, which is where the error is coming from.

After looking into it a little, it looks like the latest Datejs version doesn't add its own Date.now() function anymore. Maybe you were using an older version when it worked?

Try new Date().toString('T') instead, which should work either way.

查看更多
登录 后发表回答