2014-09-30 62 views
1

我試圖從兩個日期中獲取天數,小時數。我搜索瞭解決方案並嘗試了一些如下所示的代碼,但沒有一個返回小時數爲2天,3天hours.My字段的值是這樣的:從js中的兩個日期獲取天數和時間

d1 = '2014-10-09 08:10:56'; 
d2 ='2014-11-09 10:10:56'; 



var dateDiff = function (d1, d2) { 
    var diff = Math.abs(d1 - d2); 
    if (Math.floor(diff/86400000)) { 
     return Math.floor(diff/86400000) + " days"; 
    } else if (Math.floor(diff/3600000)) { 
     return Math.floor(diff/3600000) + " hours"; 
    } else if (Math.floor(diff/60000)) { 
     return Math.floor(diff/60000) + " minutes"; 
    } else { 
     return "< 1 minute"; 
    } 
}; 
function DateDiff(date1, date2) { 
    var msMinute = 60*1000, 
    msDay = 60*60*24*1000, 
    c = new Date(),    /* now */ 
    d = new Date(c.getTime() + msDay - msMinute); 
    return Math.floor(((date2 - date1) % msDay)/msMinute) + ' full minutes between'; //Convert values days and return value  
} 

我在做什麼wrong.Any幫助的感謝

回答

0

它可以在所有的瀏覽器。 試試這個 - >

d1 = '2014-10-09 08:10:56'; 
 
    d2 = '2014-11-09 10:10:56'; 
 
    var diff = dateDiff(d1,d2); 
 
    alert(diff); 
 

 
    function splitDate(d1){ 
 
     var dSplit = d1.split(' '); \t 
 
     d = dSplit[0] + 'T' + dSplit[1]; 
 
     return d; 
 
    } 
 

 
    function dateDiff(d1,d2){ 
 
     d1 = splitDate(d1); 
 
     d2 = splitDate(d2); 
 
     var date1 = new Date(d1); 
 
     var date2 = new Date(d2); 
 
     var dateDiff = new Date(date2 - date1);  
 
     var diff = "Month " + dateDiff.getMonth() + ", Days " + dateDiff.getDay() + ", Hours " + dateDiff.getHours(); 
 
     return diff; 
 

 
    }

+0

謝謝你,但你的代碼顯示'月NaN,日NaN,小時NaN'。你測試過了嗎? – Adda 2014-09-30 09:21:37

+0

它在鉻瀏覽器中工作。 – 2014-09-30 09:26:25

+0

爲什麼不在Firefox中? – Adda 2014-09-30 09:27:01

0

我假設你想2個日期之間的差異。

var dateDiff = function(d1/*String*/, d2/*String*/){ 
     var date1 = new Date(d1); 
     var date2 = new Date(d2); 
     var result = { 
      negative:false 
     }; 
     var diff = date1-date2; 
     if(diff<0){ 
     result.negative = true; 
     diff*=-1; 
     } 

     result.milliseconds = diff%1000; 
     diff-=result.milliseconds; 
     diff/=1000; 

     result.seconds = diff%60; 
     diff-=result.seconds 
     diff/=60; 

     result.minutes = diff%60; 
     diff-=result.minutes 
     diff/=60; 

     result.hours = diff%24; 
     diff-=result.hours 

     result.days= diff/=24; 
     //And so on 
     return result; 
} 
+0

我得到了'[object Oobject]'輸出。我應該期待什麼答案? – Adda 2014-09-30 09:26:36

+0

你正在提醒輸出。 您可以通過訪問 var op = dateDiff(d1,d2); alert(op.days | +「hours」+ op.hours); – Neo 2014-09-30 09:37:24

1

你有沒有調用函數dateDiff之前轉換d1, d2到Date對象?因爲如果你沒有,這行var diff = Math.abs(d1 - d2);將無法​​按預期工作。

UPDATE:

我'假設你d1d2"Y-m-d H:S:M"格式,試試這個:

function parseDate(str){ 
    var tmp = str.split(' '); 
    var d = tmp[0].split('-'); 
    var t = tmp[1].split(':'); 
    return new Date(d[0], d[1]-1, d[2], t[0], t[1], t[2]); 
} 

function dateDiff(d1, d2){ 
    d1 = parseDate(d1); 
    d2 = parseDate(d2); 
    // ... 
    // Your code continues 
} 
+0

@Unihedron,看起來對我來說是一個非常好的答案 - 是對還是錯是另一回事,但它提供了一個潛在的解決方案。 – 2014-09-30 09:36:53

+0

@Unihedron,對不起,我之前沒有提供解決方案,只是更新了我的答案。 – 2014-09-30 09:45:11

0

我不知道如果我真的理解,但我認爲這是你想要什麼:http://jsfiddle.net/OxyDesign/927n0L34/

JS

var difference = toDaysAndHours('2014-10-09 08:10:56','2014-11-09 10:10:56'); 

function toDaysAndHours(d1,d2){ 
    var dif, hours, days, difString = ''; 
    d1 = new Date(d1); 
    d2 = new Date(d2); 
    dif = Math.abs(d1 - d2); 
    hours = (dif/(1000*60*60)).toFixed(0); 
    days = (hours/24).toFixed(0); 
    hours = hours - days*24; 
    difString = days+' days, '+hours+' hours'; 
    return difString; 
} 
+0

你的小提琴結果:NaN days,NaN hours' – Adda 2014-10-02 07:02:44

+0

有逗號而不是分號。我修好了它。它現在對你有用嗎?謝謝 – OxyDesign 2014-10-02 10:00:40

1

我希望我能使它變得更簡單......但是這似乎起作用。

var d1 = '2014-10-09 08:10:58', 
    d2 ='2015-10-09 08:10:50'; 

function getDateFromString(str) { 
    var regexDate = /([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})/, 
    values = regexDate.exec(str); 
    return new Date(values[1], values[2], values[3], values[4], values[5], values[6]); 
} 
function daysInMonth(month,year) { 
    return new Date(year, month, 0).getDate(); 
} 

function dateDiff(d1,d2){ 
    if (d1.getTime() > d2.getTime()) { 
     var oldD1 = d1; 
     d1 = d2; 
     d2 = oldD1; 
    } 
    var yearDiff = d2.getFullYear() - d1.getFullYear(), 
     monthDiff = d2.getMonth() - d1.getMonth(), 
     dayDiff = d2.getDate() - d1.getDate(), 
     hourDiff = d2.getHours() - d1.getHours(), 
     minDiff = d2.getMinutes() - d1.getMinutes(), 
     secDiff = d2.getSeconds() - d1.getSeconds(); 

    if (secDiff < 0) { 
     secDiff = 60 + secDiff; 
     minDiff--; 
    } 
    if (minDiff < 0) { 
     minDiff = 60 + minDiff; 
     hourDiff--; 
    } 
    if (hourDiff < 0) { 
     hourDiff = 24 + hourDiff; 
     dayDiff--; 
    } 
    if (dayDiff < 0) { 
     var days = daysInMonth(date2.getMonth(), date2.getFullYear()); 
     dayDiff = days + dayDiff; 
     monthDiff--; 
    } 
    if (monthDiff < 0) { 
     monthDiff = 12 + monthDiff; 
     yearDiff--; 
    } 

    var diff = yearDiff > 0 ? yearDiff + " years " : ""; 
    diff += monthDiff > 0 ? monthDiff + " months " : ""; 
    diff += dayDiff > 0 ? dayDiff + " days " : ""; 
    diff += hourDiff > 0 ? hourDiff + " hours " : ""; 
    diff += minDiff > 0 ? minDiff + " minutes " : ""; 
    diff += secDiff > 0 ? secDiff + " seconds " : ""; 
    return diff; 

} 

var date1 = getDateFromString(d1), 
    date2 = getDateFromString(d2) 

document.getElementById('test').innerHTML += date1 + "<br />" + date2; 

document.getElementById('test').innerHTML += "<br />" + dateDiff(date1, date2); 

console.log(dateDiff(date1, date2)); 

JSFiddle

0

在第一個功能你正在使用的字符串沒有日期。正確初始化日期使用構造函數,如:

d1 = new Date('2014-10-09 08:10:56') 
d2 = new Date('2014-11-09 10:10:56') 

然後在D1和D2可以使用所有的get * /套*此處指定方法:http://www.w3schools.com/jsref/jsref_obj_date.asp

雖然我得到分心寫答案,我看到別人都說過類似的東西,但我會補充一點:

數據對象的方法很好理解事物,但是如果你想省時用http://momentjs.com/或者類似的模塊來節省時間和錯誤。

相關問題