Date.js:Date.now() 行为奇怪
我正在使用 date.js。
行 time_container.innerHTML = Date.now().toString ('T');
工作得很好,简单,现在在 Firebug 控制台中抛出错误:基数必须是至少 2 且不大于 36 的整数
。它肯定更早地发挥作用。
注意:date.js toString()
函数使用特殊的格式说明符。
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()
只是 document.getElementById()
的快捷方式。 app.onload()
是(正如您可能猜到的)onload 函数。
注释掉的行导致了问题。注释下方的替换行有效,但没有给出我想要的格式。 T
应输出 h:mm:ss tt
(小时、分钟、秒、上午/下午)。缺少 am/pm 位。
另外,我确信 Date.now()
今天早些时候可以正常工作。也许我会尝试摆弄电脑时钟,看看这是否会有所不同。
包含的 date.js 版本是 date-en-IE.js
。代码中声称的日期是 2008-05-13,尽管我今天早些时候从 SVN 结账中得到了它。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ECMAScript 5 已经有一个
Date.now()
函数,它返回自 1970 年 1 月 1 日以来的毫秒数。显然您正在调用该版本,因此toString('T') 调用是针对数字,而不是
Date
对象。Number.prototype.toString
只能接受 2 到 36 之间的数字作为参数,这就是错误的来源。经过一番研究,最新的 Datejs 版本似乎不再添加自己的
Date.now()
函数。也许您在使用旧版本时可以正常工作?尝试使用
new Date().toString('T')
代替,无论哪种方式都应该有效。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 thetoString('T')
call is on a number, not aDate
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.我在 FireFox 中也遇到了同样的错误。
通过将 .toString() 方法更改为 .toDateString() 似乎解决了这个问题。
示例:Date.now().toDateString('M/d/yyyy HH:mm')
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')