Finding time between dates and hours

Results 1 to 2 of 2

Thread: Finding time between dates and hours

  1. #1
    Join Date
    Dec 1969

    Default Finding time between dates and hours

    How can I find the time between two different time/dates using Javascript? <BR><BR>ie..<BR><BR>07/04/2002 3:41AM to 07/06/2002 1:20AM<BR><BR>All I need is the hour value. I don&#039;t need to worry about min or sec.

  2. #2
    Join Date
    Dec 1969

    Default RE: Finding time between dates and hours

    Hi, <BR><BR>I built a function similar to what you require, but days instead of hours. It might be overkill.. I based it on this function:<BR><BR><BR><BR><BR>function compareDates(date1, date2)<BR>{<BR>/************************************************** *************<BR>/ The start date is date1, the end date is date2<BR>/ It parses out the required fields to build a real date w/Date()<BR>/ Finally sending in the info the the timeDifference() function<BR>/ returning a positive or negative number. If negative, <BR>/ this means that the end date was smaller than starting date<BR>****************************************** *********************/<BR><BR> //simply parse out the values of the 2 dates.<BR> var date1Array = date1.split(&#039;/&#039;); <BR> var day1 = date1Array[1];<BR> var month1 = date1Array[0];<BR> var year1 = date1Array[2];<BR> <BR> var date2Array = date2.split(&#039;/&#039;); <BR> var day2 = date2Array[1];<BR> var month2 = date2Array[0];<BR> var year2 = date2Array[2];<BR> <BR> var earlierdate = new Date(year1,month1-1,day1); var laterdate = new Date(year2,month2-1,day2); <BR> <BR> return timeDifference(laterdate,earlierdate);<BR>}<BR><BR ><BR>function timeDifference(laterdate,earlierdate) {<BR><BR>/************************************************** *************<BR>/ Compares the 2 date objects, getting a millisecond value<BR>/ Finally returning the actual days elapsed between the 2.<BR>******************************************** *******************/<BR><BR> //difference is the time in milliseconds<BR> var difference = laterdate.getTime() - earlierdate.getTime();<BR><BR> //to get number of days, we must convert ms to days.<BR> var daysDifference = Math.floor(difference/1000/60/60/24);<BR> return daysDifference;<BR>}<BR><BR>The thing you will have to change is really the built date, and the returning date type, mine being Days.<BR><BR>You would do this:<BR><BR>[hl="yellow"]<BR>var hoursDifference = Math.floor(difference/1000/60/60);<BR>difference -= hoursDifference*1000*60*60<BR>[/hl]<BR><BR>Hope this helps.<BR><BR>-Vadim C.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts