javascript Date()。getTime()不是一个函数 [英] javascript Date().getTime() is not a function

查看:168
本文介绍了javascript Date()。getTime()不是一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



由于某种原因,我正在获得2012年5月1日星期二16:43:03 GMT + 0900 (JST)没有方法'getTime'



当然,字符串没有方法



我在回调中开始使用这段代码,但是在 getTime()在创建 var age 的行:

  for(var i = 0; i  store.get(array_of_usage_indices [i] ['key'],function(may_need_gc) 
if(may_need_gc){
var now = Date();
var created = Date(may_need_gc ['value']);
var age = now.getTime() created.getTime();
}
})
}

我已经把它放下了,所以我的例子页面就是这样:

 &l t;!DOCTYPE html PUBLIC -  // W3C // DTD XHTML 1.0 Transitional // ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> 
< html xmlns =http://www.w3.org/1999/xhtml>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = UTF-8/>
< title>日期测试< / title>

< / head>

< body>

< script type =text / javascriptcharset =utf-8>
var now = Date();
alert(now.getTime());
var t = Date()。getTime();
< / script>
< / body>
< / html>

Chrome 18.0.1025.168和Firefox 13.0中失败。



我尝试过的截图:







所以我的问题:



wth? >



我必须使用ParseDate()吗?为什么这不工作?

解决方案

尝试使用新的关键字实例化一个新对象
,而不是这个

  var now = Date(); 

尝试这个

 code> var now = new Date(); 


I'm trying to compare some Dates in javascript.

For some reason, I'm getting "Tue May 01 2012 16:43:03 GMT+0900 (JST) has no method 'getTime'"

Of course, strings don't have methods

I started with this code inside a callback, but it was failing at getTime() on the line that creates var age:

for (var i = 0; i < array_of_usage_indices.length; i++) {
    store.get(array_of_usage_indices[i]['key'],function(may_need_gc) {
        if(may_need_gc) {
            var now = Date();
            var created = Date(may_need_gc['value']);
            var age = now.getTime()-created.getTime();
        }
    })
}

I've pared it down so my example page is literally just this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>date test</title>

    </head>

<body>

<script type="text/javascript" charset="utf-8">
            var now = Date();
            alert(now.getTime());
            var t = Date().getTime();
</script>    
</body>
</html>

This is failing in Chrome 18.0.1025.168 and Firefox 13.0.

Screenshots of what I've tried:

So my question:

wth?

Do I have to use ParseDate()? Why isn't this working?

解决方案

Try using new keyword to instantiate a new object so instead of this

var now = Date();

try this

var now = new Date();

这篇关于javascript Date()。getTime()不是一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆