2017-03-27 29 views
0

我想寫的代碼來獲得「日期」不包括週末和銀行假期(這意味着當我輸入日期和天數,它應該排除週末和銀行假期,比如我給予03-24-2017和3天,假設第29個假期是假期,應該給予03-31-2017)。我得到了整個事情的代碼,但它採取了今天的日期,但我希望手動輸入日期,我試着用document.getElementById(「今天」)。但它不給任何輸出。提前感謝。代碼排除週末銀行假期

<!DOCTYPE html> 
 
<html lang="en"> 
 
<head> 
 
<meta charset="UTF-8" /> 
 

 
<title> Add Days and Holidays </title> 
 

 
</head> 
 
<body> 
 

 
<pre id="fourWeeks"></pre> 
 
Add <input id="nDaysToAdd" value="0"> days<p> 
 
<button onclick="calcBusinessDay()"> Calculate 
 

 
</button> 
 
<div id="debug"</div> 
 

 
<script type="text/javascript"> 
 

 
var today = new Date(); 
 
var holidays = [ 
 
    [2017,2,10], 
 

 
before holiday 
 
    [2017,7,4], 
 
]; 
 

 
Date.prototype.addDays = function (days) { return new 
 

 
Date(this.getTime() + days*24*60*60*1000); } 
 

 
Date.prototype.addBusAndHoliDays = function (days) { 
 
    var cDate = this; 
 
    var holiday = new Date(); 
 
    var c='', h=''; 
 
    for (var i=1; i<=days ; i++){ 
 
    cDate.setDate(cDate.getDate() + 1); 
 
    if (cDate.getDay() == 6 || cDate.getDay() == 0) { 
 

 
days++; } 
 
\t else { 
 
\t for (j=0; j<holidays.length; j++) { 
 
\t  holiday = new 
 

 
Date(holidays[j][0],(holidays[j][1]-1),holidays[j][2]); 
 
\t \t c = cDate.toDateString(); h = 
 

 
holiday.toDateString(); 
 
\t  if (c == h) { days++; } 
 
\t } 
 
\t } 
 
    } return cDate; 
 
} 
 

 
    
 
Date.prototype.DayList = function (daysToShow) { 
 
    var td = this; 
 
    if (daysToShow == undefined) { daysToShow = 31; } 
 

 
    var str = ''; 
 
    for (var i=0; i<daysToShow; i++) { 
 
    newday = new 
 

 
Date(td.getFullYear(),td.getMonth(),(td.getDate()+i)); 
 
    str += newday.toDateString()+'\t =>\t'+i+' actual days 
 

 
ahead<br>'; 
 
    } return str; 
 
} 
 

 
function calcBusinessDay() { 
 

 
functions 
 
    var today = new Date(); 
 
    var N = 
 

 
parseInt(document.getElementById('nDaysToAdd').valu 
 

 
e) || 0; 
 
    
 
    var wd = today.addDays(N); 
 
    document.getElementById('debug').innerHTML 
 
    = '<p>'+N+' week days from today 
 

 
('+today.toDateString() +') will be on: 
 

 
'+wd.toDateString(); 
 

 
    var bd = today.addBusAndHoliDays(N); 
 
    document.getElementById('debug').innerHTML += 
 

 
'<p>'+N+' business days will be on: '+bd.toDateString(); 
 

 
    str += '<p>'+newDay.DayList(14); 
 
    document.getElementById('debug').innerHTML += 
 

 
'<p>'+str; 
 
    
 

 
    
 
} 
 
</script> 
 

 
</body> 
 
</html>

+1

並且其中的代碼是?你在這方面迄今爲止嘗試過什麼? –

回答

0

隨着document.getElementById("today").value你會得到一個輸入HTML元素的值,而不必與今天價值的任何輸入。

下面是一個代碼段得到今天的日期(警告:日期被格式化d/M/Y)

<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <meta charset="utf-8"> 
 
    <meta name="viewport" content="width=device-width"> 
 
    <title>JS Bin</title> 
 
    <script src="https://cdn.rawgit.com/JDMcKinstry/JavaScriptDateFormat/master/Date.format.min.js"></script> 
 
</head> 
 
<body> 
 
    
 
    
 
    Today: <input id="today" type="text"> 
 
    <br> 
 
    <button onclick="calculate()" >Calculate something</button> 
 
    
 
    <div id="results" style="margin-top: 20px; border: red;"></div> 
 
    
 
    <script> 
 
    document.getElementById("today").value = new Date().format('d/m/Y'); 
 
    
 
    function calculate() { 
 
     var splitted = document.getElementById("today").value.split("/"); 
 
     var today = new Date(splitted[2], splitted[1] - 1, splitted[0]);  
 
     document.getElementById("results").innerHTML = "Today is " + today; 
 
     
 
    } 
 
    </script> 
 
    
 
</body> 
 
</html>