【问题标题】:how to compare the current date with the input date in java script or jquery如何将当前日期与javascript或jquery中的输入日期进行比较
【发布时间】:2017-05-23 08:01:21
【问题描述】:

我正在使用cleandersonlobo:date-picker-materialize 包输入日期,如果输入日期小于当前日期,我想显示alert("expired")

var $strDate = $(".pmt-date").val();
var $dtDate = $.datepicker.parseDate("mm/dd/yy", $strDate);
var $strToday = $.datepicker.formatDate('mm/dd/yy', new Date());
var $tDate = $.datepicker.parseDate('mm/dd/yy', $strToday);

if ($dtDate < new Date()) {
  $('#myModal1').modal('hide');
  alert("Job expired");
}

【问题讨论】:

    标签: javascript jquery date


    【解决方案1】:

    试试下面的代码,

    var $strDate = $(".pmt-date").val();
    var $dtDate = $.datepicker.parseDate("mm/dd/yy", $strDate);
    var $strToday = $.datepicker.formatDate('mm/dd/yy', new Date());
    var $tDate = $.datepicker.parseDate('mm/dd/yy', $strToday);
    
    if ($dtDate < $strToday ) {
      $('#myModal1').modal('hide');
      alert("Job expired");
    }
    

    【讨论】:

      【解决方案2】:

      toMidnight 函数将日期的时间设置为午夜,这样可以消除时间作为比较因素:

      function toMidnight(date) {
        var midnight = arguments.length > 0 ? new Date(date) : new Date();
        midnight.setHours(0);
        midnight.setMinutes(0);
        midnight.setSeconds(0);
        midnight.setMilliseconds(0);
        return midnight;
      }
      

      然后你可以像这样比较你的日期:

      toMidnight($dtDate) < toMidnight()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-05-15
        • 2019-08-21
        • 2013-06-12
        • 1970-01-01
        • 2015-08-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多