有人能提出一种方法来比较两个大于、小于和过去不使用JavaScript的日期的值吗?值将来自文本框。


当前回答

与往常一样比较<和>,但涉及==或==的任何内容都应使用+前缀。像这样:

const x=新日期(‘2013-05-23’);const y=新日期(‘2013-05-23’);//小于、大于即可:console.log('x<y',x<y);//假的console.log('x>y',x>y);//假的console.log('x<=y',x<=y);//真的console.log('x>=y',x>=y);//真的console.log('x===y',x===y);//假的,哎呀!//任何涉及“==”或“===”的内容都应使用“+”前缀//然后将比较日期的毫秒值console.log('+x===+y',+x===+y);//真的

其他回答

通过Moment.js

Jsfidle:http://jsfiddle.net/guhokemk/1/

function compare(dateTimeA, dateTimeB) {
    var momentA = moment(dateTimeA,"DD/MM/YYYY");
    var momentB = moment(dateTimeB,"DD/MM/YYYY");
    if (momentA > momentB) return 1;
    else if (momentA < momentB) return -1;
    else return 0;
}

alert(compare("11/07/2015", "10/07/2015"));

如果dateTimeA大于dateTimeB,则该方法返回1

如果dateTimeA等于dateTimeB,则该方法返回0

如果dateTimeA小于dateTimeB,则该方法返回-1

以上给出的所有答案只解决了一件事:比较两个日期。

事实上,它们似乎是这个问题的答案,但其中很大一部分是缺失的:

如果我想检查一个人是否满18岁怎么办?

不幸的是,上面给出的答案中没有一个能够回答这个问题。

例如,当前时间(大约是我开始键入这些单词的时间)是2020年1月31日星期五10:41:04 GMT-0600(中部标准时间),而客户将其出生日期输入为“01/31/2002”。

如果我们使用“365天/年”,即“31536000000”毫秒,我们将得到以下结果:

       let currentTime = new Date();
       let customerTime = new Date(2002, 1, 31);
       let age = (currentTime.getTime() - customerTime.getTime()) / 31536000000
       console.log("age: ", age);

打印如下:

       age: 17.92724710838407

但从法律上讲,这位客户已经18岁了。即使他输入“01/30/2002”,结果仍然是

       age: 17.930039743467784

其小于18。系统将报告“未成年”错误。

“01/29/2002”、“01/28/2002”和“01/27/2002”。。。“01/05/2002”,直到“01/04/2002”。

这样的系统只会杀死所有出生在18岁0天到18岁26天之间的客户,因为他们在法律上是18岁,而系统显示“未成年”。

以下是对类似问题的回答:

invalidBirthDate: 'Invalid date. YEAR cannot be before 1900.',
invalidAge: 'Invalid age. AGE cannot be less than 18.',

public static birthDateValidator(control: any): any {
    const val = control.value;
    if (val != null) {
        const slashSplit = val.split('-');
        if (slashSplit.length === 3) {
            const customerYear = parseInt(slashSplit[0], 10);
            const customerMonth = parseInt(slashSplit[1], 10);
            const customerDate = parseInt(slashSplit[2], 10);
            if (customerYear < 1900) {
                return { invalidBirthDate: true };
            } else {
                const currentTime = new Date();
                const currentYear = currentTime.getFullYear();
                const currentMonth = currentTime.getMonth() + 1;
                const currentDate = currentTime.getDate();
                if (currentYear - customerYear < 18) {
                    return { invalidAge: true };
                } else if (
                    currentYear - customerYear === 18 &&
                    currentMonth - customerMonth < 0) {
                    return { invalidAge: true };
                } else if (
                    currentYear - customerYear === 18 &&
                    currentMonth - customerMonth === 0 &&
                    currentDate - customerDate < 0) {
                    return { invalidAge: true };
                } else {
                    return null;
                }
            }
        }
    }
}

你可以用最简单易懂的方式进行比较。

<input type="date" id="getdate1" />
<input type="date" id="getdate2" />

假设您有两个要比较的日期输入。

因此,首先编写一个通用的日期解析方法。

 <script type="text/javascript">
            function parseDate(input) {
             var datecomp= input.split('.'); //if date format 21.09.2017

              var tparts=timecomp.split(':');//if time also giving
              return new Date(dparts[2], dparts[1]-1, dparts[0], tparts[0], tparts[1]);
// here new date(  year, month, date,)
            }
        </script>

parseDate()是解析日期的常用方法。现在您可以检查date=、>、<任何类型的比较

    <script type="text/javascript">

              $(document).ready(function(){
              //parseDate(pass in this method date);
                    Var Date1=parseDate($("#getdate1").val());
                        Var Date2=parseDate($("#getdate2").val());
               //use any oe < or > or = as per ur requirment 
               if(Date1 = Date2){
         return false;  //or your code {}
}
 });
    </script>

当然,这段代码会帮助你。

注意-仅比较日期部分:

当我们在javascript中比较两个日期时。这需要数小时、数分钟和数秒的时间。。因此,如果我们只需要比较日期,这是一种方法:

var date1= new Date("01/01/2014").setHours(0,0,0,0);

var date2= new Date("01/01/2014").setHours(0,0,0,0);

现在:如果date1.valueOf()>date2.valueOf。

function datesEqual(a, b)
{
   return (!(a>b || b>a))
}