简体   繁体   English

检查日期自今天起30天以上

[英]Check Date greater than 30 days from today's date

I am using jQuery datepicker and tried to find out difference between todays date and selected date , but getting issues... rather than issues... I was not able to find it perfectly... 我正在使用jQuery datepicker并试图找出今天日期和所选日期之间的区别,但是遇到问题......而不是问题......我无法完美地找到它...

I tried to do this on 'onSelect event of datepicker ' 我尝试在'onSelect event of datepicker'上做这个

Question: How to check whether selected Date using jQuery Datepicjer is greater than 30 days from todays date ? 问题:如何使用jQuery Datepicjer检查所选日期是否超过今天的30天?

Any help will be appreciated....!! 任何帮助将不胜感激....!! note: dont want to use any libraries, I need to solve this by using only jQuery. 注意:不想使用任何库,我需要通过仅使用jQuery来解决这个问题。

Get the timestamp for 30 days from now: 获取30天后的时间戳:

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

Compare that timestamp with the timestamp for the selected date. 将该时间戳与所选日期的时间戳进行比较。

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.
}

I have created one sample for you. 我为你创建了一个样本。

 <!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>

and Js 和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');
  }
});

You can check this code online Here 您可以检查此码在线在这里

Try this: 试试这个:

$('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..
        }

    },
});

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM