检查日期超过2天,即日期javascript

时间:2015-02-23 06:36:13

标签: javascript html date

我需要检查日期是否比当前日期大两天增加计数并显示

JS代码:

> var exceeds = 0;
>          var date = "25-02-2015";
>          var today = new Date();
>           if((new Date(today.getFullYear(), today.getMonth(), today.getDate()+2))>date) {    exceed+=1; }
> 
> console.log(exceeds);

3 个答案:

答案 0 :(得分:0)

var exceeds = 0;
var date = "25-02-2015", date_val  = date.split("-");

// Getting timestamp of date
var otherDate = new Date(+date_val[2], +date_val[1], +date_val[0]).getTime();

// Get the timestamp for 2 days from now:
var timestamp = new Date().getTime() + (2* 24 * 60 * 60 * 1000)
if(otherDate>timestamp) exceeds++;

答案 1 :(得分:0)

var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();

if(dd<10) {
    dd='0'+dd
} 

if(mm<10) {
    mm='0'+mm
} 

today = mm+'/'+dd+'/'+yyyy;
document.write(today);
var currentDate = "03/23/2015";
if(today <= currentDate ){
alert('yes');
}

答案 2 :(得分:0)

您可以将日期转换为Unix TimeStamp,即从1970-01-01 00:00:00到您的特定日期的毫秒数。 好吧,有代码

var yourDate = new Date(Date.UTC(2015, 02, 25)).getTime(); //Get the timestamp 
var today = new Date().getTime();         
if(today - yourDate > 60 * 60 * 1000 * 24 * 2){alert('greater than 2 days')}