简体   繁体   English

如何使用JavaScript或jQuery限制日期?

[英]How to restrict date using JavaScript or jQuery?

I have two date field in my webpage 1- fromDate and 2- toDate. 我的网页中有两个日期字段,分别是fromdate和2-toDate。 I want to restrict date on submit if the differences b/w date is greater than 7. 如果差异黑白日期大于7,我想限制提交日期。

在此处输入图片说明

I have applied my script but not working... 我已经套用了指令码,但无法运作...

   var d1 = new Date();
   d1 = document.forms["From"]["fromDate"].value;
   var d2 = new Date();
   d2 = document.forms["From"]["toDate"].value;
   if ((d2 - d1) > 7) {
       alert("Please enter valid date range");
       return false;
   }

Finally What I Want: if a user enter the date and the difference is greater than 7 days then it will show an alert. 最后,我要的是:如果用户输入日期,并且差异大于7天,则它将显示警报。 Example: FromDate = 01/01/2016 and ToDate= 01/10/2016 if ToDate - FromDate then its show an alert on submit 示例:如果ToDate-FromDate,则FromDate = 01/01/2016,ToDate = 01/10/2016,则在提交时显示警报

In the above example (ToDate = 01/10/2016) - (FromDate = 01/01/2016) = 9 在上面的示例中(ToDate = 01/10/2016)-(FromDate = 01/01/2016)= 9

try something like 尝试类似

        var d1 = new Date(document.forms["From"]["fromDate"].value);
        var d2 = new Date(document.forms["From"]["toDate"].value);
        if ((d2 - d1) / 1000 / 3600 / 24 > 7) {
            alert("Please enter valid date range");
            return  false;
        }

1000 - 1000 ms in every second 每秒1000-1000毫秒

3600 - 3600 in every hour 每小时3600-3600

24 - 24 hours in every day 每天24至24小时

var Start = moment([2016, 3, 25]);
var End = moment([2016, 3, 27]);
var Different = End.diff(Start, 'days')   // = 2

if(parseInt(Different) > 7){
    alert("Please enter valid date range");
    return  false;
}

From Document : http://momentjs.com/docs/ 从文档: http : //momentjs.com/docs/

Date Pickers usually return a string formatted as MM/DD/YYYY. 日期选择器通常返回格式为MM / DD / YYYY的字符串。

This function returns a YYYYMMDD integer. 此函数返回YYYYMMDD整数。 It assumes dates are padded with zero when there is a single digit (eg 01, not 1). 假设只有一位数字(例如01,而不是1)时,日期用0填充。 Usually there is a setting for the date picker to return the date zero padded. 通常,日期选择器会设置为返回零填充日期。

function dateFormatYYYYMMDD( num ) {
  return parseInt( num.toString().slice(6,10) + num.toString().slice(0,2) + num.toString().slice(3,5) );
};

Then to find the difference, subtract your first date from the second date. 然后找出差异,从第二个日期减去您的第一个日期。 eg 例如

d1 = "12/01/2016";
d2 = "12/07/2016";
diff = dateFormatYYYYMMDD(d2) - dateFormatYYYYMMDD(d1);

In this example, diff holds the value 6 . 在此示例中, diff保留值6

The only hitch is when the diff goes over a month end, since a month can end on different days. 唯一的障碍是差异持续一个月结束,因为一个月可以在不同的日子结束。

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

相关问题 如何在javascript / jquery中限制开始日期之前的结束日期选择 - How to restrict the selection of end date before the start date in javascript / jquery 使用jquery中的开始日期限制结束日期 - restrict end date using start date in jquery 如何使用 JQuery 动态更改日期选择器并限制到不同的日期范围? - How to dynamically change datepicker and restrict to different date ranges using JQuery? 如何限制仅使用javascript或jQuery上传视频? - How to restrict only video upload using javascript or Jquery? 如何使用JavaScript / jQuery同时限制多个用户登录 - How to restrict multiple user logins at same time using JavaScript / jQuery 如何使用Javascript / JQuery在移动浏览器中限制点运算符('。') - How to restrict dot operator('.') in mobile browser using Javascript/ JQuery 如何使用 jquery/javascript 在 ckeditor textarea 中首先限制 position 的空间 - How to restrict space at first position in ckeditor textarea using jquery/javascript 使用jQuery / javascript限制价格字段输入 - using jQuery/javascript to restrict price field input 如何使用 JavaScript/jQuery 格式化现有日期 - How to format existing date using JavaScript/jQuery 如何使用jQuery / Javascript更改日期格式? - How to change date format using jQuery/Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM