2015-07-20 22 views
0

在我的班次報告中,有StartTime和EndTime的字段。我有他們之間的差異,但我actully想知道EndTime是否是另一天。使用時間差異的JQuery想知道第二次是同一天還是另一天

例如,時鐘時間爲上午09:00,ClockOut時間爲上午12:30,因此總工作時間爲15:20。現在我需要把它分成兩排。這意味着如果一天改變,那麼我需要顯示兩行,如同第一行相同的日期,我必須顯示工作時間是15:00,下一個日期我必須顯示工作時間是00:20。我已經通過使用下面的功能有所區別。

function diffTime(start, end) { 

    var timeStart = new Date("01/01/2007 " + start); 
    var timeEnd = new Date("01/01/2007 " + end); 
    var seconds = Math.floor((timeEnd - (timeStart))/1000); 
    var minutes = Math.floor(seconds/60); 
    var hours = Math.floor(minutes/60); 
    var days = Math.floor(hours/24); 

    hours = hours - (days * 24); 
    minutes = minutes - (days * 24 * 60) - (hours * 60); 
    seconds = seconds - (days * 24 * 60 * 60) - (hours * 60 * 60) - (minutes * 60); 

    return (hours < 9 ? "0" : "") + hours + ":" + (minutes < 9 ? "0" : "") + minutes + ":" + (seconds < 9 ? "0" : "") + seconds; 
} 

現在請任何人告訴我,如何根據我的要求在兩個不同的日期區分這一點。

回答

0

jQuery不能真正幫助你進行日期操作。但是你不需要它。

var sameDay = timeStart.getDate() == timeEnd.getDate(); // Will tell you if the 2 dates are on the same day 
var sameMonth = timeStart.getMonth() == timeEnd.getMonth(); // Will tell you if the 2 dates are on the same month 
var sameYear = timeStart.getFullyear() == timeEnd.getFullyear(); // Will tell you if the 2 dates are on the same day 

if (!sameDay && !sameMonth && !sameYear) { 
    // timeStart and timeEnd are not in the same day. Do whatever you want. 
} 
0

如果你把日期的副本,您可以使用setHours()設置自己的時間部分爲0,這樣單獨的日期比較:

startdate.setHours(0,0,0,0); 
    enddate.setHours(0,0,0,0); 
    if (startDate != enddate){ 

參考:http://www.w3schools.com/jsref/jsref_sethours.asp

或者僅使用日期/時間值的年份,月份和日期來構建日期。

startDate = new Date(timeStart.getFullYear(), timeStart.getMonth(), timeStart.getDate()); 
    endDate = new Date(timeEnd.getFullYear(), timeEnd.getMonth(), timeEnd.getDate()); 
    if (startDate != enddate){ 
相關問題