简体   繁体   中英

JavaScript simple date validation

I try to validate a date entered by the user. It must be today or a later date. How I can do that?

Why the condition in the code below is false ?

 var today = new Date();
 var idate = new Date('02/09/2014');

 if(today > idate) {
   alert('your date is big');
 }

If I set today then it is today's date and also I pass in idate then it is also today's date then how can I compare dates?

Here is JSFiddle: http://jsfiddle.net/0osh0q8a/1/

A few things to consider.

When you're creating a new Date object from a string representation, do it in the format YYYY-MM-DD . This will avoid problems with locale.

When comparing two dates, if the time can be ignored, set both to the exactly same time. It looks to be the case here.

Finally, use Date.parse() to make sure your object is a valid date and make it possible to be compared.

var today = new Date();
var idate = new Date('2014-09-02');
// The date entered by the user will have the same
// time from today's date object.
idate.setHours(today.getHours());
idate.setMinutes(today.getMinutes());
idate.setSeconds(today.getSeconds());
idate.setMilliseconds(today.getMilliseconds());

// Parsing the date objects.
today = Date.parse(today);
idate = Date.parse(idate);

// Comparisons.
if (idate == today) {
    alert('Date is today.');
}
else if (idate < today) {
    alert('Date in the past.');
}
else if (idate > today) {
    alert('Date in the future.');
}

Demo

As a side note, when you face hard-to-solve date/time calculations, manipulations, etc, you can use the Moment.js library. It's really useful: Moment.js

the default data parser is reading your idate as 9th febuary 2014, hence today is greater than idate

If you set idate to 09/04/2014 the code runs as expected

 var today = new Date();
 var idate = new Date('09/04/2014');

 console.log(today);
 >>>Tue Sep 02 2014 11:48:52 GMT+0100 (BST)
 console.log(idate);
 >>>Thu Sep 04 2014 00:00:00 GMT+0100 (BST) 

You have 2 problems.

The date culture and the time part.

First off, new Date() picks-up the current date, with the current browser's culture plus the time part.

new Date('09/04/2014') does not add a time part, so it starts at 00:00:00 and the culture again depends on the browser. So it may mean 9th of March or 4th of Sept depending on culture.

Remember, that new Date() contains the time part. If you don't care about the time, create the today date like this:

var now = new Date();
var today = new Date(now.getFullYear(), now.getMonth(), now.getDay());

Another thing is that JS date formatting is 'mm/dd/yyyy'. So change your 'idate' like this:

var idate = new Date('09/02/2014');

You can use < and > to compare the dates. But == will always return false, to check if 2 dates are equal use: if(today.getTime() == idate.getTime()) See the updated fiddle: http://jsfiddle.net/0osh0q8a/3/

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