簡體   English   中英

如何在javascript中兩個日期時間之間計算天數?

[英]how to count days between two date time in javascript?

如何計算兩個日期之間的天數?
請在下面查看我的代碼:

<script type="text/javascript">
    // how to count days between two date time ?
    var a= '2014-03-21 12:00:12';
    var b = '2014-05-11 18:00:00';
    function days_diff(a,b){
        // .....
        // ..... placeholder ....
        // ..... placeholder ....
        // ..... placeholder ....
        // ..... placeholder ....
        // ..... placeholder ....
        // .....
        // var days = days_a - days_b;
        return days;
    }

</script>   
  <script>
    var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
    var firstDate = new Date(2008,01,12);
    var secondDate = new Date(2008,01,22);
    var diffDays = Math.round(Math.abs((firstDate.getTime()-secondDate.getTime())/(oneDay)));
  </script>

嘗試這個

<input id="first" value="1/1/2000"/>
<input id="second" value="1/1/2001"/>

<script>
  alert(datediff("day", first, second)); // what goes here?
</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)
}

alert(daydiff(parseDate($('#first').val()), parseDate($('#second').val())));

這可能對您有幫助...

var Date1 = new Date (2012, 6, 12);
var Date2 = new Date (2014, 3, 16);
var Days = Math.floor((Date2.getTime() - Date1.getTime())/(1000*60*60*24));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM