简体   繁体   English

JavaScript简单日期验证

[英]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 ? 为什么以下代码中的条件为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? 如果我设置了today那么它就是今天的日期,并且我输入了idate那么它也是今天的日期,那么我该如何比较日期呢?

Here is JSFiddle: http://jsfiddle.net/0osh0q8a/1/ 这是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 . string表示形式创建新的Date对象时,请使用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. 最后,使用Date.parse()来确保您的对象是有效日期,并可以进行比较。

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. 附带说明一下,当您面对难以解决的日期/时间计算,操作等问题时,可以使用Moment.js库。 It's really useful: Moment.js 这真的很有用: Moment.js

the default data parser is reading your idate as 9th febuary 2014, hence today is greater than idate 默认数据解析器读取你的idate作为第九febuary 2014年,因此today大于idate

If you set idate to 09/04/2014 the code runs as expected 如果将idate设置为2014年9月4日,则代码将按预期运行

 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. 您有2个问题。

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()获取当前日期,以及当前浏览器的区域性和时间部分。

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. new Date('09/04/2014')没有添加时间部分,因此它从00:00:00开始,并且区域性再次取决于浏览器。 So it may mean 9th of March or 4th of Sept depending on culture. 因此,根据文化的不同,可能意味着3月9日或9月4日。

Remember, that new Date() contains the time part. 请记住, new Date()包含时间部分。 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'. 另一件事是,JS日期格式为“ mm / dd / yyyy”。 So change your 'idate' like this: 因此,像这样更改您的“ idate”:

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

You can use < and > to compare the dates. 您可以使用< and >比较日期。 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/ 但是==总是返回false,以检查两个日期是否相等: if(today.getTime() == idate.getTime())请参阅更新的小提琴: http : //jsfiddle.net/0osh0q8a/3/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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