简体   繁体   中英

JavaScript Check Date not today or in the past

JavaScript Check Date not today or in the past.

var c = '10 JUN 2010'; // this is the format of the date coming in.
var temp = new Array();
temp = c.split(' ');

var x = new Date ( temp[1]+" "+temp[0]+", "+temp[2] );

if (x.getTime() > getDate()) {
   alertstring = alertstring + '\n\nDEBUG CODE: 1 ' + x + '\n\n';
}

I cannot change the format coming in.

Looks like you 99% have it. Here it is with a modified if condition:

var c = '10 JUN 2010'; // this is the format of the date coming in.
var temp = new Array();
temp = c.split(' ');

var x = new Date ( temp[1]+" "+temp[0]+", "+temp[2] );

if (x.getTime() > (new Date().getTime())) {
    ...
}

Update this line:

// Get current date and time
var today = new Date();

// strip time to compare to the parse date
if (x.getTime() > new Date(today.getFullYear(), today.getMonth(), today.getDate()).getTime()) {
    // this date has not happened yet.
    alertstring = alertstring + '\n\nDEBUG CODE: 1 ' + x + '\n\n';
}

Try to put in constructor the number of month Instead of string . Instead of June put 6.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM