无法将日期和时间与当前时间进行比较?

时间:2023-01-21 20:20:08

I am using bootstrap date time picker library and getting value from document like this.

我正在使用bootstrap日期时间选择器库并从这样的文档中获取值。

var accountExpiry = jQuery(".tempExpiry input").val(); 

returns = "12 October 2015 - 03:35 PM"

return =“2015年10月12日 - 03:35 PM”

and my goal is to compare accountExpiry time with Date.now() and alert user Expiry must greater than current time.

我的目标是将accountExpiry时间与Date.now()进行比较,并且警告用户Expiry必须大于当前时间。

I have tried following jquery but that is wrong:

我试过跟随jquery,但那是错的:

var accountExpiry = jQuery(".tempExpiry input").val();
if (accountExpiry > Date.now()) { 
    console.log("OK");
} else { 
    console.log("Select time must be greater than current"); 
}

1 个解决方案

#1


0  

Demo

var accountExpiry = "12 October 2015 - 08:35 AM"; //change this with jQuery(".tempExpiry input").val()
var date = new Date(Date.parse(accountExpiry.replace(" -",""))) ;
if (date > new Date(Date.now())) { 
    console.log(date , new Date(Date.now()) ,"less");
} else { 
    console.log(date , new Date(Date.now()) ,"greater"); 
}

#1


0  

Demo

var accountExpiry = "12 October 2015 - 08:35 AM"; //change this with jQuery(".tempExpiry input").val()
var date = new Date(Date.parse(accountExpiry.replace(" -",""))) ;
if (date > new Date(Date.now())) { 
    console.log(date , new Date(Date.now()) ,"less");
} else { 
    console.log(date , new Date(Date.now()) ,"greater"); 
}