Date.js: Date.now() behaving oddly

心已入冬 提交于 2020-01-13 03:30:52

问题


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.


回答1:


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.




回答2:


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')



来源:https://stackoverflow.com/questions/7349284/date-js-date-now-behaving-oddly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!