jQuery UI日历的最小/最大日期差异 [英] jQuery UI min/max date difference for calendar

查看:112
本文介绍了jQuery UI日历的最小/最大日期差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个插件,在预订表单上使用jquery UI为其日历,并希望显示一个警报,并且不允许提交表单,如果第二日期较少,例如从第一天7天



所以 [01.01.2012] + [02.01.2012] 不会工作




[01.01.2012] + [15.01.2012] 工作,任何想法如何我可以通过jquery achive这?非常感谢!

解决方案

jsFiddle DEMO



基本上这里的jist是你需要 Date.parse 两个日期(将它们带入毫秒),然后将这些毫秒除以/ 1000(获得秒)/ 60(获得分钟)/ 1440(获得日)。

$ b $

  $(b 

然后测试一下是否超过7, '#submit')。on('click',function(){
var toDate = Date.parse($('#toDate')。val()),
fromDate = Date.parse $('#fromDate')。val()),
difference = toDate - fromDate;

var days = difference / 1000/60/1440;

if(days <7){
alert('请在两个日期之间至少间隔7天!');
return false; //停止提交
}

// VALID
else {
alert('success!');
}
});
pre>

I am using a plugin which uses jquery UI for its calendar on a booking form and would like to display an alert, and not allow the form to be submitted if either the 2nd date is less that lets say 7 days from first date, or if it is over 30 days from first date.

so [01.01.2012] + [02.01.2012] would not work

but [01.01.2012] + [15.01.2012] would work, any idea how I can achive this through jquery? Many thanks in advance!

解决方案

jsFiddle DEMO

Basically the jist here is that you need to Date.parse() both dates (which brings them into milliseconds) then divide those millseconds by / 1000 (to get seconds) / 60 (to get minutes) / 1440 (to get days).

Then test to see if it's over 7 and boom you're all set!

$('#submit').on('click', function () {
    var toDate = Date.parse($('#toDate').val()),
        fromDate = Date.parse($('#fromDate').val()),
        difference = toDate - fromDate;

    var days = difference / 1000 / 60 / 1440;

    if ( days < 7 ) {
        alert('Please give at least 7 days between the dates!');
        return false; // stop from submitting        
    }

    // VALID
    else {
        alert('success!');
    }
});​

这篇关于jQuery UI日历的最小/最大日期差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆