nezbo Posted February 10, 2009 Share Posted February 10, 2009 Hi all I am trying to get the difrence between 2 date in both decimal format and time format and useing innerHTML post it back to the page. The porblem i cam getting is that the value is not coming back correctly var StartHours = ((StartTimesHoursMin1 < 1000) ? "0" : "") + StartTimesHoursMin1; var StartHours1 = StartHours.substring(2,0); var StartMins1 = StartHours.substring(4,2); var EndHours = ((EndTimesHoursMin1 < 1000) ? "0" : "") + EndTimesHoursMin1; var EndHours1 = EndHours.substring(2,0); var EndMins1 = EndHours.substring(4,2); var curStartTime = new Date(StartYears1,StartMonth1,StaryDays1,StartHours1,StartMins1,00); var curEndTime = new Date(EndYears1,EndMonth1,EndDays1,EndMins1,EndHours,00); var one_day=60*60*24*365; //var curDateTime = Math.ceil((curStartTime.getTime()-curEndTime.getTime())/(one_day)); document.write(((curEndTime.getTime()-curStartTime.getTime())/(one_day))) var curHour = curDateTime.getHours() var curMin = curDateTime.getMinutes() var curTime = ((curHour < 10) ? "0" : "") + curHour + ":" + ((curMin < 10) ? "0" : "") + curMin; var decimalTime = (curHour + curMin/60); document.getElementById("theIHours").value = decimalTime; document.getElementById("hoursANDMins").innerHTML = curDateTime; I am not to sure where i have gone wrong. Please Help Nezbo Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 10, 2009 Share Posted February 10, 2009 Assuming you have your start and end dates as data objects, I have a function that will give you the difference in days, hours, minutes, etc: //****************************************************************// // FUNCTION: dateDiff(Date1Obj, Date2Obj, [units], [precision]) // // // // Returns the difference between two date objects in the units // // specified (optional, default is days). The optional precision // // parameter determines the number of decimal places the result // // will be rounded to. Note: When the 'days' units is used the // // precision is not applicable (will determine the difference in // // calendar days). // // // // The units parameter can include the following: d=days, // // h = hours, m = minutes, s = seconds, ms = milliseconds // //****************************************************************// function dateDiff(date1Obj, date2Obj, units, precision) { //set the default untis var units = (units)?units:'d'; var precision = (precision && units!='d')?Math.pow(10, precision):1; //Calculate the units divisor switch (units) { case 'ms': //Milliseconds var units = 1; break; case 's': //Seconds var units = 1000; break; case 'm': //Minutes var units = 1000 * 60; break; case 'h': //hours var units = 1000 * 60 * 60; break; case 'd': //Calendar Days var units = 1000 * 60 * 60 * 24; //Normalize time to 12:00am to count calendar days date1Obj.setHours(0); // = new Date(Date1Obj.getFullYear(), Date1Obj.getMonth(), Date1Obj.getDate()); date2Obj.setHours(0); // = new Date(Date2Obj.getFullYear(), Date2Obj.getMonth(), Date2Obj.getDate()); break; } If you have hours in a decimal format, it would be fairly simple to reformat in an hh:mm format: function decToHours (hoursInDec) { var minsInt = Math.round(hoursInDec * 60); var hours = Math.round(minsInt/60); var mins = (minsInt - (hours * 60)); mins = (mins<10) ? '0'+mins : mins; return (hours+':'+mins); } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.