javascript Date().getTime() is not a function

匿名 (未验证) 提交于 2019-12-03 01:49:02

问题:

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 

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

               date test

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?

回答1:

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

var now = Date(); 

try this

var now = new Date(); 


回答2:

You need to use the new operator to create a Date object.

(new Date()).getTime() 


回答3:

This will make 'now' as variable type as date:

var now = new Date(); 

This will get you time from 'now':

new Date(now).getTime(); 


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