2012-02-13 13 views
0
Date.prototype.copy = function() { 
    return new Date(this.getTime()); 
}; 


Date.prototype.msPERDAY = 1000 * 60 * 60 * 24; 
Date.prototype.msPERHOUR = 1000 * 60 * 60; 

    Date.prototype.getDaysBetween = function(d) { 

    d = d.copy(); 

    var diff = d.getTime() - this.getTime(); 

    var days = Math.floor((diff)/this.msPERDAY); 
    diff = (diff)/this.msPERDAY; 
    var hours = Math.floor((diff)/this.msPERHOUR); 
    alert(hours); 
    var time = new Array(days , hours); 
    return time; 
}; 

私は将来の日付を取得し、現在の日付でこの機能ではJavaScript日付デバイド

future.dateそれを使用して - current.date =私は日間の差を取得ここでの違い

をミリ秒が、私は申し訳ありませんF、時間、分、秒

あなたは私は、これはjavascriptのある

日の間の差を取得した後に分割する必要があるか、私に言うことができますを取得するカントまたは愚かな質問だが、あなただけ diff = (diff)/this.msPERDAYの結果の小数部分をつかむと、24で(: http://jsfiddle.net/ZLUqB/もjsfiddle上):以下試してみてくださいことを掛けることができ、私にとっては問題:(時間については

答えて

0

ある

Date.prototype.copy = function() { 
    return new Date(this.getTime()); 
}; 


Date.prototype.msPERDAY = 1000 * 60 * 60 * 24; 
Date.prototype.msPERHOUR = 1000 * 60 * 60; 

Date.prototype.getDaysBetween = function(d) { 

    d = d.copy(); 

    var diff = d.getTime() - this.getTime(); 
    console.log(diff); 

    var days = Math.floor((diff)/this.msPERDAY); 
    console.log(days); 

    diff = (diff)/this.msPERDAY % 1; // get only the decimal part of this 
    console.log(diff); 

    var hours = diff * 24; // multiply the decimal part by 24 to get hours 
    console.log(hours); 

    var time = new Array(days , hours); 
    return time; 
}; 

var d = new Date('02/01/2012 05:00'); 
d.getDaysBetween(new Date('02/12/2012 13:00'));