【问题标题】:Check Date greater than 30 days from today's date检查日期从今天起超过 30 天
【发布时间】:2013-01-29 11:15:13
【问题描述】:

我正在使用 jQuery datepicker 并试图找出今天日期和选定日期之间的区别,但是遇到问题......而不是问题......我无法完美找到它......

我尝试在“日期选择器的 onSelect 事件”上执行此操作

问题: 如何检查使用 jQuery Datepicjer 选择的日期是否大于今天的 30 天?

任何帮助将不胜感激....!! 注意:不想使用任何库,我只需要使用 jQuery 来解决这个问题。

【问题讨论】:

    标签: javascript jquery jquery-ui


    【解决方案1】:

    获取从现在起 30 天的时间戳:

    var timestamp = new Date().getTime() + (30 * 24 * 60 * 60 * 1000)
    //                                      day hour  min  sec  msec
    

    将该时间戳与所选日期的时间戳进行比较。

    if (timestamp > selectedTimestamp) {
        // The selected time is less than 30 days from now
    }
    else if (timestamp < selectedTimestamp) {
        // The selected time is more than 30 days from now
    }
    else {
        // -Exact- same timestamps.
    }
    

    【讨论】:

    • 纯代码,但 OP 似乎要求未来 30 天,而不是 30 天前。对我来说很明显这只是一个标志变化,但可能想澄清未来的绊脚石。
    【解决方案2】:

    我为您创建了一个示例。

     <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="utf-8" />
      <title>jQuery UI Datepicker - Default functionality</title>
      <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.0/themes/base/jquery-ui.css" />
      <script src="http://code.jquery.com/jquery-1.8.3.js"></script>
      <script src="http://code.jquery.com/ui/1.10.0/jquery-ui.js"></script>
      <link rel="stylesheet" href="/resources/demos/style.css" />
      <script>
      $(function() {
        $( "#datepicker" ).datepicker();
      });
      </script>
    </head>
    <body>
    
    Date: <input type="text" id="thedate"/>
    <div id="checkDate">Check Date</div>
    </body>
    </html>
    

    和Js

    $('#thedate').datepicker();
    
    $('#checkDate').bind('click', function() {
      var selectedDate = $('#thedate').datepicker('getDate');
      var today = new Date(); 
      var targetDate= new Date();
      targetDate.setDate(today.getDate()+ 30);
      targetDate.setHours(0);
      targetDate.setMinutes(0);
      targetDate.setSeconds(0);
    
      if (Date.parse(targetDate ) >Date.parse(selectedDate)) {
        alert('Within Date limits');
      } else {
        alert('not Within Date limits');
      }
    });
    

    您可以在线查看此代码Here

    【讨论】:

      【解决方案3】:

      试试这个:

      $('input').datepicker({
          onSelect: function()
          {
              var date = $(this).datepicker('getDate');
              var today = new Date();
              if((new Date(today.getFullYear(), today.getMonth(), today.getDate()+30))>date)
              {
                   //Do somthing here..
              }
      
          },
      });
      

      演示:http://jsfiddle.net/jeY7S/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-11-15
        • 2016-09-11
        • 1970-01-01
        • 2011-10-31
        • 1970-01-01
        • 1970-01-01
        • 2011-09-26
        • 2014-09-13
        相关资源
        最近更新 更多