2015-10-14 88 views
0

我有一個腳本,用於計算兩個給定日期之間的星期數。如何匹配JavaScript中給定日期差異的日期

然後創建一個表格,其中行數等於星期數。

腳本看起來像這樣

JSFIDDLE

腳本

$('#test').click(function() { 

    // Here are the two dates to compare 
    var date1 = '29-10-2015'; 
    var date2 = '29-12-2015'; 
    var Targetvalue = parseFloat("1000000"); 
     var dealjson = '[{"dealdate":"25-11-2015","cost":200000}]'; 

    // First we split the values to arrays date1[0] is the year, [1] the month and [2] the day 
    date1 = date1.split('-'); 
    date2 = date2.split('-'); 

    // Now we convert the array to a Date object, which has several helpful methods 
    date1 = new Date(date1[2], date1[1], date1[0]); 
    date2 = new Date(date2[2], date2[1], date2[0]); 

    // We use the getTime() method and get the unixtime (in milliseconds, but we want seconds, therefore we divide it through 1000) 
    date1_unixtime = parseInt(date1.getTime()/1000); 
    date2_unixtime = parseInt(date2.getTime()/1000); 

    // This is the calculated difference in seconds 
    var timeDifference = date2_unixtime - date1_unixtime; 

    // in Hours 
    var timeDifferenceInHours = timeDifference/60/60; 

    // and finaly, in days :) 
    var timeDifferenceInDays = timeDifferenceInHours/24; 
    var timeDifferenceInWeeks = Math.round(timeDifferenceInDays/7); 
    // alert(timeDifferenceInDays/7); 
    TargetPerweek = Targetvalue/timeDifferenceInWeeks; 
    //Math.round(timeDifferenceInWeeks); 
    TargetPerweek = Math.round(TargetPerweek * 100)/100; 
    var string = "<table data-role='table' class='ui-responsive'><thead><tr><th>Week</th><th>Target</th><th>Achieved</th></tr></thead>"; 
    for (var i = 1; i <= timeDifferenceInWeeks; i++) 
    string = string + "<tr><th>Week" + i + "</th><td>" + TargetPerweek + "</td><td></td></tr>"; 

    string = string + "</table>"; 
    $('.varianceData').html(string); 

}); 

HTML

<button id="test">See the Tab</button> 
<div class="varianceData"></div> 

如果Ç舔小提琴中的按鈕,您將看到一張帶有目標的桌子,並實現每週明智的價值。

所以我想表明在各自的一週,他做了交易

基礎上實現了可變dealjson所以在achived專欄中,我應該顯示在各自的一週達到的量;基於該dealjson

有望走出看跌

<table data-role="table" class="ui-responsive"> 
    <thead> 
     <tr> 
      <th>Week</th> 
      <th>Target</th> 
      <th>Achieved</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
      <th>Week1</th> 
      <td>111111.11</td> 
      <td>No Deal</td> 
     </tr> 
     <tr> 
      <th>Week2</th> 
      <td>111111.11</td> 
      <td>No Deal</td> 
     </tr> 
     <tr> 
      <th>Week3</th> 
      <td>111111.11</td> 
      <td></td> 
     </tr> 
     <tr> 
      <th>Week4</th> 
      <td>111111.11</td> 
      <td>No Deal</td> 
     </tr> 
     <tr> 
      <th>Week5</th> 
      <td>111111.11</td> 
      <td>200000</td> 
     </tr> 
     <tr> 
      <th>Week6</th> 
      <td>111111.11</td> 
      <td></td> 
     </tr> 
     <tr> 
      <th>Week7</th> 
      <td>111111.11</td> 
      <td>No Deal</td> 
     </tr> 
     <tr> 
      <th>Week8</th> 
      <td>111111.11</td> 
      <td>No Deal</td> 
     </tr> 
     <tr> 
      <th>Week9</th> 
      <td>111111.11</td> 
      <td>No Deal</td> 
     </tr> 
    </tbody> 
</table> 
+0

您的預期產出是多少? –

+0

@ManProgrammer我更新了我的問題請看看 – Vikram

+0

你怎麼能決定**沒有交易**顯示與否 –

回答

1

您可以使用setDate()增加date1 7天數,直到它變成等於或大於date2更大。並使用簡單的>= && <=比較交易日期,以檢查日期是否在一週內,您將需要一個臨時變量。 這裏是更新的fiddle。它沒有優化,但它的工作。

+0

所以現在如果我有更多數量的Json中的存檔值,它應該工作正確嗎? – Vikram

+1

是的小提琴中的代碼應該適用於dealjson中的一系列交易。 –

+0

實際上在目標星期明智應該是目標/ totalweek你能告訴我怎麼可以在這裏做 – Vikram