简体   繁体   English

Javascript:最短日期始终小于任何其他日期

[英]Javascript: the minimum date that is always less than any other date

I want to compare strings that are dates 我想比较日期字符串

if (user_date < date)

date has yyyy-mm-dd format, so it is OK to compare dates as strings, if the user enters a valid user_date in this format. date具有yyyy-mm-dd格式,因此如果用户以这种格式输入有效的user_date ,则可以将日期作为字符串进行比较。

Unfortunately, user_date is entered by the user, so it could be invalid. 不幸的是, user_date由用户输入,因此它可能无效。 In case if it is invalid (or left empty), I want (user_date < date) always to be true. 万一无效(或留空),我希望(user_date <date)始终为true。

I have found to set var user_date=''; 我发现设置了var user_date=''; if user's date is invalid. 如果用户的日期无效。 Is that a good way to make make (user_date < date) for any valid date? 这是使make (user_date < date)为任何有效日期的好方法吗?

The same question is for (user_date > date) (more than any date). 对于(user_date > date) (大于任何日期),存在相同的问题。 I've found to set var user_date='A'; 我发现设置var user_date='A';

I need it for 我需要它

if (date1 <= date && date2>= date)
//do something

So it works without any additional if then . 因此, if then没有的if then它可以正常工作。

PS I don't want to enter var user_date='2222-12-31'; PS我不想输入var user_date='2222-12-31'; because the algorythm will stop working properly in less than 210 years. 因为算法将在不到210年的时间内停止正常工作。

为什么不将最小值设置为0000-01-01,将最大值设置为9999-12-31?

I think that is not possible. 我认为那是不可能的。 But what is possible is to get a date for which a comparison is always false — so functionally it behaves the same as NaN does for numbers. 但是可能的是获得一个比较总是错误的日期-因此在功能上,它的行为与NaN对于数字的行为相同。 So if you invert the test, thus replace (a <= b) with !(a > b) , it would work. 因此,如果反转测试,然后将(a <= b)替换为!(a > b) ,它将起作用。

$date = new Date(NaN);
if (!($date1 > $date) && !($date2 < $date))  ...

ps What's with the dollar signs? ps美元符号是什么? Are you confusing Javascript with PHP or Perl? 您是否将Javascript与PHP或Perl混淆了? :) :)

I think you can do somethin glike 我想你可以做些喜欢的事

var date = '2013-03-12';
var $date = new Date(date);
function onChange(el){
    var user_date = el.value;
    var udate = new Date(user_date);
    if(isNaN(udate.getTime()) || udate < $date){
        alert('less')
    }

}

Demo: Fiddle 演示: 小提琴

Personally I would first validate the date that the user is entering - don't leave it to chance. 我个人将首先验证用户输入的日期-不要让它偶然。

I use my own date extensions library here for this kind of stuff: 我在这里使用自己的日期扩展库来进行此类操作:

DP_DateExtensions DP_DateExtensions

The parseFormat() method will let you easily validate your input. parseFormat()方法将使您轻松验证输入。 Something like this so do well: 这样的事情做得很好:

Date.parseFormat(InputString, "YYYY-M-D")

The function returns a valid JavaScript date if the entered string matches the format or null if it doesn't. 如果输入的字符串与格式匹配,则该函数返回有效的JavaScript日期;否则,返回null。 You obviously don't have to send a user message based on this if you have a default behavior you'd like to apply - but the code itself should "know". 如果您具有要应用的默认行为,则显然不必基于此发送用户消息-但是代码本身应该“知道”。

In this case - if you want an invalid date to be less than another, known good date, then there are many ways to do it. 在这种情况下-如果您希望一个无效的日期小于另一个已知的有效日期,那么有很多方法可以做到这一点。 Most simple is something like this: 最简单的是这样的:

InputDate = Date.parseFormat(InputString, "YYYY-M-D");
if ( InputDate || InputDate < GoodDate ) {
     Input Date is Invalid or Falls before good date
} else {
     Input Date is Falls after good date
};

You don't need to set "false dates" or guess with the right validation and Boolean logic in play and it will work until the world stops spinning (in theory). 您无需设置“错误日期”或使用正确的验证和布尔逻辑进行猜测,它就可以起作用,直到世界停止旋转为止(理论上)。

You can you use the compare() method in the library to do any kind of greater/less than comparison to any precision. 您可以使用库中的compare()方法对任何精度进行大于/小于比较。

Hope this helps. 希望这可以帮助。

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

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