Javascript 从今天的日期起超过 30 天的检查日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14581610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 17:17:30  来源:igfitidea点击:

Check Date greater than 30 days from today's date

javascriptjqueryjquery-ui

提问by Pratik Bhatt

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 '

我试图在 datepicker 的“onSelect 事件”上执行此操作

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 来解决这个问题。

回答by Cerbrus

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

回答by Sandeep

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

您可以在此处在线查看此代码

回答by Harish Anchu

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/