我正在尝试比较今天的日期和字符串中的日期。它们都具有字符串类型。我为什么得到“不!” ?

jQuery(document).ready(function () {
    Date.prototype.today = function () {
        return ((this.getDate() < 10) ? "0" : "") + this.getDate() + "/" +(((this.getMonth() + 1) < 10) ? "0" : "") + (this.getMonth() + 1) + "/" + this.getFullYear();
    }

    datetodayvar = new Date().today();
    deadlinadate = '16/10/2016';

    if (String(datetodayvar) >= String(deadlinadate)) {
        alert("yes!");
    } else {
        alert("no!");
    }
});

最佳答案

将它们都变成Date对象而不是字符串。



jQuery(document).ready(function () {
    Date.prototype.today = function () {
        return ((this.getDate() < 10)?"0":"") + this.getDate() +"/"+(((this.getMonth()+1) < 10)?"0":"") + (this.getMonth()+1) +"/"+ this.getFullYear();
    }

    datetodayvar = new Date().today();
    deadlinadate = '02/11/2016';

    if(new Date(datetodayvar) >= new Date(deadlinadate)) {
        alert("yes!");
    } else {
        alert("no!");
    } });

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>





如果您有3个不同的输入字段(下拉列表或其他内容)中的“日期字符串”,则不要输入字符串,而是按以下格式设置日期格式。

var year = 2015;
var month = 2-1; // February
var day = 27;
var now = new Date(year, month, day);


这样,如果您需要使用a-a,则不必担心日期符号,本地化。或/或两者之间的其他内容。

还要记住,月份始终为-1,因为它开始计数为0(januari为0,12月为11。

另外,请记住日光节约时间。减去一个小时,这也可能会使您刚铸造的日期对象变得毫无用处。

下面的代码段包含了我将在“简单”比较机制中使用的所有内容。


jQuery(document).ready(function () {
        var str = '';
        for(var c=1;c<32;c++) {
            str += '<option value="'+c+'">'+c+'</option>';
        }
        $('#day').html(str);
        var str = '';
        for(var c=0;c<12;c++) {
            str += '<option value="'+c+'">'+(c+1)+'</option>';
        }
        $('#month').html(str);
        var str = '';
        for(var c=parseInt(new Date().getFullYear());c>1990;c--) {
            str += '<option value="'+c+'">'+c+'</option>';
        }
        $('#year').html(str);
        $('#istodaycheck').on('click',function() {
            var day = $('#day').get(0);
            var month = $('#month').get(0);
            var year = $('#year').get(0);

            var date = new Date(
                         year.options[year.selectedIndex].value,
                         month.options[month.selectedIndex].value,
                         day.options[day.selectedIndex].value);
            date.correctDst();

            $('#output').text(date.isToday() ? 'yes' : 'no');
        });
     });

/**
 * Retrieve standard timezome offset
 */
Date.prototype.stdTimezoneOffset = function() {
   var jan = new Date(this.getFullYear(), 0, 1);
   var jul = new Date(this.getFullYear(), 6, 1);
   return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
}
/**
 * Checks if date is in day lights savings
 */
Date.prototype.dst = function() {
   return this.getTimezoneOffset() < this.stdTimezoneOffset();
}
/**
 * corrects the unwanted substraction of an hour on fresh dates.
 */
Date.prototype.correctDst = function() {
    if(!this.dst()) {
         this.setHours(this.getHours()+1);
     }
}
/**
 *  Returns a date instance without time components.
 */
Date.prototype.justDate = function() {
     var date = new Date(this.getFullYear(),this.getMonth(),this.getDate());
     date.correctDst();
     return date;
}
/**
 * Tests if given date is today.
 */
Date.prototype.isToday = function() {
    // strip possible time part.
    var testdate = this.justDate();
    var datetodayvar = new Date().justDate();
    return datetodayvar.getTime() == testdate.getTime()
}
     

#output {
  background-color: #eee;
  border: 1px solid pink;
  display: block;
  width: 100%;
  height:200px;
  text-align: center;
  font-size:121pt;

}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="day">
</select>
<select id="month">
</select>
<select id="year">
</select>
<button id="istodaycheck">Is this date today?</button>
<div id="output">
</div>

10-05 20:53
查看更多