开发者

JavaScript test if date (in string format) is more than 30 days ago

开发者 https://www.devze.com 2022-12-10 21:22 出处:网络
i have the date in a string format like so \'11/2/2009\' (m/d/yyyy) I need to test if that is greater than 30 days ago.

i have the date in a string format like so '11/2/2009' (m/d/yyyy)

I need to test if that is greater than 30 days ago.

Whats the easiest and least error-prone way to do this.开发者_如何学C


Something like this perhaps:

var then = new Date("11/2/2009").getTime(),
    now  = new Date().getTime(),
    thirtyDaysInMilliseconds = 2592000000;
if (now - then > thirtyDaysInMilliseconds) { doSomething(); }


You'll have to be sure your date meets the format (MM-DD-YYYY), but this should work:

var olddate = "11/02/2009"
var dt = Date.parse( olddate );
return ( ( Date.getTime() - dt.getTime() ) < 2592000000 );
0

精彩评论

暂无评论...
验证码 换一张
取 消