开发者

How can I check that a selected date is within a 30 day period?

开发者 https://www.devze.com 2023-03-14 06:27 出处:网络
I\'m trying to validate that the selected date is within 30 days of today\'s date. How can I do this in jQuery? Here\'s what I have so far:

I'm trying to validate that the selected date is within 30 days of today's date. How can I do this in jQuery? Here's what I have so far:

<input ty开发者_如何学Pythonpe="text" id="txtMaxDate" />
<input type="submit" onclick="validateMaxDate();" />

<script type="text/javascript">
    function validateMaxDate() {
    // format of sendDate is 05/25/2011
    var sendDate = $("#txtMaxDate").val();
    var fullDate = new Date()
    var currentDate = fullDate.getMonth()+1 + "/" + fullDate.getDate() + "/" + fullDate.getFullYear();
    var newSendDate = sendDate.UTC();
    alert(newsendDate);


    }
</script>


This should work if target date has previously been set as a future date.

var today = new Date();
targetDate.setDate(targetDate.getDate() - 30);

if(targetDate <= today){
  alert('target date is less than 30 days out');
}


var oneDay = 1000*60*60*24; \\one day has these many milli seconds
var diff   = (today.getTime() - sendDate.getTime())/oneDay \\send date and today are date objects
if(Math.abs(diff)<30){alert("with in 30 days");


The date calculation piece isn't so much jQuery as it is native JavaScript.

You are on the right track with timestamps. In my opinion, the most straightforward way is to compare timestamps by doing the following:

  • Subtract currentDate from maxDate
  • See if the difference is less than 30 days

See a quick example - http://jsfiddle.net/6YQHQ/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号