简体   繁体   English

日期范围选择

[英]date Range selection

i was trying to get the number of days between fromDate and toDate as user should not select more than 90 days. 我试图获取fromDate和toDate之间的天数,因为用户不应选择超过90天。 I am using dojo to get the date calendar. 我正在使用dojo获取日期日历。 Below is the code i tried but the output was not as expected, please suggest. 以下是我尝试过的代码,但输出与预期不符,请提出建议。

<script>
function parseDate(str) {
    var mdy = str.split('-')
    return new Date(mdy[2], mdy[0]-1, mdy[1]);
}

function daydiff(first, second) {
    return (second-first)/(1000*60*60*24);
}

function daysDifference(){
    var fromDate = document.getElementById("fromDate").value;
    var toDate = document.getElementById("toDate").value;
    alert(fromDate);
alert("days difference" + daydiff(parseDate(fromDate),parseDate(toDate)));
}
</script>
<form id="myform" name="myform" action="checkData.htm">


    From Date:<input type="text" name="fromDate" id="fromDate" value="" data-dojo-type="dijit/form/DateTextBox" required="true" constraints="{ datePattern: 'dd-MM-yyyy'}" /> </td>
    To Date:<input type="text" name="toDate" id="toDate" value="" data-dojo-type="dijit/form/DateTextBox" required="true" constraints="{ datePattern: 'dd-MM-yyyy'}"/> </td>
    <input type="submit" value="submit" onclick="daysDifference();"/>

</form>

Please suggest how can i restrict user to select the date range between 90days. 请建议我如何限制用户选择90天之间的日期范围。

You may try like this: 您可以这样尝试:

var fromDate = new Date("7/11/2014");
var toDate = new Date("12/12/2015");
var timeDiff = Math.abs(toDate.getTime() - fromDate.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
if(diffDays > '90')
alert("Select a date range in 90 dates from toDate");

JSFIDDLE DEMO JSFIDDLE演示

The Date.parse() method returns the number of returns the number of milliseconds between January 1, 1970. You could run parse on each date and then get the difference of the values through subtraction. Date.parse()方法返回1970年1月1日之间返回的毫秒数。您可以在每个日期运行parse,然后通过减法获得值的差。 You would then need to convert the time in milliseconds to a days. 然后,您需要将时间(以毫秒为单位)转换为几天。

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

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