如何将日期与当前日期进行比较以应用逻辑

How to compare date with current date for applying logic

本文关键字:比较 应用逻辑 当前日期 日期      更新时间:2023-09-26
<script>
$(document).ready(function(){
    if(date.now()>($("[id$=clear2]").val)+2){
    $("[id$=clear]").val("");
    $("[id$=clear2]").val("");// date value
    $("[id$=clear3]").val("");
    }
});
</script>
我想检查当前日期(日/月/年(

是否大于日期(日/月/年(值 + 2 天。我正在处理几种情况,通过删除如果条件它工作正常.通过使用它 工作不好 .你能展示一些解决方案,以便我继续前进

试试这个:

    var d1 = '31/11/2015'.split('/');
    var d2 = '27/12/2015'.split('/');
    var date1 = new Date(d1[2],d1[1],d1[0]); // YYYY,MM,DD
    var date2 = new Date(d2[2],d2[1],d2[0]);
    var numOfDaysToAdd = 2;
    date2.setDate(date2.getDate() + numOfDaysToAdd);
    
    if (date1.getTime() < date2.getTime()) {
       alert('date1 is before date2');
    }

在javascrip中使用日期:

JavaScript

$(document).ready(function () {
    var today = new Date();
    var tomorrow = new Date(today);
    tomorrow.setDate(today.getDate() + 1);
    $("#today").val(today.toUTCString());
    $("#tomorrow").val(tomorrow.toUTCString());
    $("#checkDate").click(function () {
        var newDate = new Date($("#today").val());
        newDate.setDate(newDate.getDate() + 2);
        var parsedTomorrow = new Date($("#tomorrow").val());
        var comRes = newDate > parsedTomorrow;
        alert(comRes);
    });
});

.HTML

<input type="text" id="today" />
<input type="text" id="tomorrow" />
<input type="button" id="checkDate" />

演示

使用 JavaScript

Date Object 比较 JavaScript 中的日期。