#1
  1. Contributing User
    ASP High Scholar (3500 - 3999 posts)

    Join Date
    Mar 2006
    Location
    South Wales
    Posts
    3,674
    Rep Power
    2043

    Calculate difference between two times


    Hi Guys,

    I apologise if this has been asked a lot here, I have tried searching for an answer but can't seem to find a working solution!!!

    I have two times in the format hh:mm and would like to find the difference between them. For example, if time1 contained 08:30 and time2 contained 12:00, I would expect the difference to return 03:30.

    Is this achievable? Any advice greatly appreciated.
  2. #2
  3. Cunning Linguist
    ASP High Scholar (3500 - 3999 posts)

    Join Date
    Mar 2005
    Location
    I used to live at home, now I stay at the house
    Posts
    3,580
    Rep Power
    429
    You should be able to find what you need on this page:

    http://www.merlyn.demon.co.uk/js-date1.htm

    Comments on this post

    • sync_or_swim agrees
    • micky agrees
    Support requests via PM will be ignored!
    Route of Queue | The General FAQ Thread | HOW TO POST A QUESTION

    Perfectly Impossible - is the eagerly awaited debut release from Jayne Sarah - please buy it - it's really good!

  4. #3
  5. Contributing User
    ASP High Scholar (3500 - 3999 posts)

    Join Date
    Mar 2006
    Location
    South Wales
    Posts
    3,674
    Rep Power
    2043
    Thanks Chief,

    Found exactly what I needed. For anybody who has a similar problem, here are the functions you need:
    Code:
    var secondsPerMinute = 60;
    var minutesPerHour = 60;
    
    function convertSecondsToHHMMSS(intSecondsToConvert) {
    var hours = convertHours(intSecondsToConvert);
    var minutes = getRemainingMinutes(intSecondsToConvert);
    minutes = (minutes == 60) ? "00" : minutes;
    var seconds = getRemainingSeconds(intSecondsToConvert);
    return hours+":"+minutes;
    }
    
    function convertHours(intSeconds) {
    var minutes = convertMinutes(intSeconds);
    var hours = Math.floor(minutes/minutesPerHour);
    return hours;
    }
    function convertMinutes(intSeconds) {
    return Math.floor(intSeconds/secondsPerMinute);
    }
    function getRemainingSeconds(intTotalSeconds) {
    return (intTotalSeconds%secondsPerMinute);
    }
    function getRemainingMinutes(intSeconds) {
    var intTotalMinutes = convertMinutes(intSeconds);
    return (intTotalMinutes%minutesPerHour);
    }
    
    function HMStoSec1(T) { // h:m:s
      var A = T.split(/\D+/) ; return (A[0]*60 + +A[1])*60 + +A[2] }
    Example usage:
    Code:
    var time1 = HMStoSec1("10:00:00");
    var time2 = HMStoSec1("12:05:00");
    var diff = time2 - time1;
    document.write(convertSecondsToHHMMSS(diff));
  6. #4
  7. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Feb 2009
    Posts
    1
    Rep Power
    0
    Wow, I have been looking for something like this for quite sometime and must say thank you to both of you for posting this stuff up. I am new to JavaScript and struggling with something that in my mind is extremely simple.

    I too need to calculate the difference between two time values, but I need to have the time difference converted into a decimal rather than displaying the difference in HH:MM:SS.

    i.e. if:
    Time 1: "10:00 AM"
    Time 2: "1:45 PM"
    then
    Time Difference: "3.75"

    So a time difference of 3 hours and 45 minutes would be entered into the field as 3.75.

    How would I adapt this script to convert the difference into a decimal and how would I make it so that it posts to a asp field I have on the page?

    Any help would be greatly appreciated!!
  8. #5
  9. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Dec 2010
    Posts
    1
    Rep Power
    0
    Code:
    function calfx(text1,text2,text3) {
    /*In this function the start time is 10:00 AM and End time is 07:00 PM.
    
    For the holidays you need to play with number of days. That thing is still remaining. If you can do it yourself by subtracting the number of days according to your holidays. If you wouldn't able to do so then I am here for you :)*/
    
    
        var second=1000, minute=second*60, hour=minute*60, day=hour*24, week=day*7;
    	var StartTime='10:00 AM';
    	var EndTime='07:00 PM';
    	var StartTimeHour='10';
    	var EndTimeHour='7';
    	var flag=false;
    	var startDateTime = new Date('12/12/2010'+' '+ StartTime);
    	var EndDateTime = new Date('12/12/2010'+' '+ EndTime);
    	
    	date1 = document.getElementById(text1).value;
        date2 = document.getElementById(text2).value;
    	
    	var dt1=date1.split(" ");
    	//CompareDate1=new Date(dt1[0]);
    	CompareDate1=dt1[0];
    	var dttt1 = dt1[1].split(":");
    	dtt1=dt1[1]+' '+dt1[2];
    	dttttt1=new Date('12/12/2010'+' '+ dtt1);
    
    	if (dttttt1 < startDateTime) {alert('Please enter first date-time equal to or greater than '+StartTime);flag=true;}
    	if (dttttt1 > EndDateTime) {alert('Please enter first date-time equal to or less than '+EndTime);flag=true;}
    	var hrs1 = new Date();
    	
    	var dt2=date2.split(" ");
    	//CompareDate2=new Date(dt2[0]);
    	CompareDate2=dt2[0];
    	var dttt2 = dt2[1].split(":");
    	dtt2=dt2[1]+' '+dt2[2];
    	dttttt2=new Date('12/12/2010'+' '+ dtt2);
    	
    	if (dttttt2 < startDateTime) {alert('Please enter second date-time equal to or greater than '+StartTime);flag=true;}
    	if (dttttt2 > EndDateTime) {alert('Please enter second date-time equal to or less than '+EndTime);flag=true;}
    	
    	date1 = new Date(date1)
    	date2 = new Date(date2)
    	diff  = new Date();
    	diff.setTime(Math.abs(date1.getTime() - date2.getTime()));
    	timediff = diff.getTime();
    	
    	weeks = Math.floor(timediff / (1000 * 60 * 60 * 24 * 7));
    	timediff -= weeks * (1000 * 60 * 60 * 24 * 7);
    
    	days = Math.floor(timediff / (1000 * 60 * 60 * 24)); 
    	timediff -= days * (1000 * 60 * 60 * 24);
    
    	hours = Math.floor(timediff / (1000 * 60 * 60)); 
    	timediff -= hours * (1000 * 60 * 60);
    
    	mins = Math.floor(timediff / (1000 * 60)); 
    	timediff -= mins * (1000 * 60);
    
    	secs = Math.floor(timediff / 1000); 
    	timediff -= secs * 1000;
    	
    var str="";
    var str1="";
    var str2="";
    var str3="";
    var str4="";
    if (weeks!=0){
    	str =weeks + " weeks ";
    }
    if (days!=0){
    		str1 =days + " days ";
    }
    if (hours!=0){
    	if (CompareDate1 != CompareDate2){
    				var hoursStart = 00;
    				var hoursend = 00;
    				if (dt1[2]=='PM'){
    					hoursStart= parseInt(EndTimeHour)-parseFloat(dttt1[0]+"."+dttt1[1],2);}
    				else{
    				EndTimeHour1=parseInt(EndTimeHour)+12;
    				hoursStart= parseInt(EndTimeHour1)-parseFloat(dttt1[0]+"."+dttt1[1],2);}
    				
    				if (dttt1[0]==12){hoursStart=(parseInt(EndTimeHour)+12)-parseFloat(12+"."+dttt1[1],2);}
    				
    				
    				if (dt2[2]=='PM'){
    					StartTimeHour1=parseFloat(dttt2[0]+"."+dttt2[1])+12;
    					hoursEnd= parseFloat(StartTimeHour1,2)-parseInt(StartTimeHour);}
    				else{
    				hoursEnd= parseFloat(dttt2[0]+"."+dttt2[1],2)-parseInt(StartTimeHour);}
    				
    				if (dttt2[0]==12){hoursEnd=parseFloat(12+"."+dttt2[1],2)-parseInt(StartTimeHour);}
    				
    				if (mins==0){
    					hours = Math.round(parseFloat(hoursStart) + parseFloat(hoursEnd),2);
    				}
    				else{	
    					hours= parseInt(hoursStart) + parseInt(hoursEnd);
    				}
    			}
    		str2 =hours + " hours ";
    }
    
    if (mins!=0){
    		if(mins<15){mins=15;}
    		if(mins>15 && mins<30){mins=30;}
    		if(mins>30 && mins<45){mins=45;}
    		if(mins>45 && mins<60){mins=60;}
    		
    		str3 =mins + " minutes ";
    }
    if (secs!=0){
    	str4 =secs + " seconds ";
    }
    
    document.getElementById(text3).value = str + str1 + str2 + str3;
    if (flag==true) {document.getElementById(text3).value=' ';}
    
    }
    
    
    <table>
    <tr height="30">
    	<td>Approved Start Date:</td><td><input type="text" name="apsd" id="apsd" value="12/23/2010 05:00:00 PM"/>
    				Approved End Date:<input type="text" name="aped" id="aped" value="12/24/2010 11:00:00 AM"/>
    				Approved Duration:<input type="text" name="apdu" id="apdu"/>
    				<a href="javascript:calfx('apsd','aped','apdu')"><img src="images/cal.gif" width="16" height="16" border="0" alt="Pick a date"/></a></td></tr></td></tr>
    </table>
    Last edited by Shadow Wizard; December 30th, 2010 at 03:09 AM. Reason: added code tags
  10. #6
  11. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Aug 2012
    Posts
    1
    Rep Power
    0
    Okay, so this is the closest to what I would like to achieve.
    However, I want to convert the result in 24 hour format.

    I would like time2 - time1 to return 9:00 in the below example. i.e. the difference in hours/minutes between 9PM and 6AM. Currently, it returns a -15:0.

    Any ideas please? Thanks in advance!

    Code:
    <script type="text/javascript">
    var secondsPerMinute = 60;
    var minutesPerHour = 60;
    
    function convertSecondsToHHMMSS(intSecondsToConvert) {
    var hours = convertHours(intSecondsToConvert);
    var minutes = getRemainingMinutes(intSecondsToConvert);
    minutes = (minutes == 60) ? "00" : minutes;
    var seconds = getRemainingSeconds(intSecondsToConvert);
    return hours+":"+minutes;
    }
    
    function convertHours(intSeconds) {
    var minutes = convertMinutes(intSeconds);
    var hours = Math.floor(minutes/minutesPerHour);
    return hours;
    }
    function convertMinutes(intSeconds) {
    return Math.floor(intSeconds/secondsPerMinute);
    }
    function getRemainingSeconds(intTotalSeconds) {
    return (intTotalSeconds%secondsPerMinute);
    }
    function getRemainingMinutes(intSeconds) {
    var intTotalMinutes = convertMinutes(intSeconds);
    return (intTotalMinutes%minutesPerHour);
    }
    
    function HMStoSec1(T) { // h:m:s
      var A = T.split(/\D+/) ; return (A[0]*60 + +A[1])*60 + +A[2] }
    
    var time1 = HMStoSec1("21:00:00");
    var time2 = HMStoSec1("06:00:00");
    var diff = (time2 - time1);
    document.write(convertSecondsToHHMMSS(diff));
    </script>
  12. #7
  13. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Jan 2014
    Posts
    2
    Rep Power
    0

    I want to take time value from input field than entering manually, pls can anyone hel


    I want to take time value from input field than entering manually, pls can anyone help me


    Originally Posted by webwizzy
    Okay, so this is the closest to what I would like to achieve.
    However, I want to convert the result in 24 hour format.

    I would like time2 - time1 to return 9:00 in the below example. i.e. the difference in hours/minutes between 9PM and 6AM. Currently, it returns a -15:0.

    Any ideas please? Thanks in advance!

    Code:
    <script type="text/javascript">
    var secondsPerMinute = 60;
    var minutesPerHour = 60;
    
    function convertSecondsToHHMMSS(intSecondsToConvert) {
    var hours = convertHours(intSecondsToConvert);
    var minutes = getRemainingMinutes(intSecondsToConvert);
    minutes = (minutes == 60) ? "00" : minutes;
    var seconds = getRemainingSeconds(intSecondsToConvert);
    return hours+":"+minutes;
    }
    
    function convertHours(intSeconds) {
    var minutes = convertMinutes(intSeconds);
    var hours = Math.floor(minutes/minutesPerHour);
    return hours;
    }
    function convertMinutes(intSeconds) {
    return Math.floor(intSeconds/secondsPerMinute);
    }
    function getRemainingSeconds(intTotalSeconds) {
    return (intTotalSeconds%secondsPerMinute);
    }
    function getRemainingMinutes(intSeconds) {
    var intTotalMinutes = convertMinutes(intSeconds);
    return (intTotalMinutes%minutesPerHour);
    }
    
    function HMStoSec1(T) { // h:m:s
      var A = T.split(/\D+/) ; return (A[0]*60 + +A[1])*60 + +A[2] }
    
    var time1 = HMStoSec1("21:00:00");
    var time2 = HMStoSec1("06:00:00");
    var diff = (time2 - time1);
    document.write(convertSecondsToHHMMSS(diff));
    </script>
  14. #8
  15. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Jan 2014
    Posts
    2
    Rep Power
    0

    I want to take time value from input field than entering manually


    Originally Posted by Sowmya RK
    I want to take time value from input field than entering manually, pls can anyone help me
    I want to take time value from input field than entering manually, pls can anyone help me
  16. #9
  17. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Jan 2014
    Posts
    1
    Rep Power
    0
    I feel my research is almost completed. Happy to see that. Thanks to share this brilliant matter.
  18. #10
  19. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Jun 2014
    Posts
    1
    Rep Power
    0
    <html>
    <head>
    <script type="text/javascript">
    var secondsPerMinute = 60;
    var minutesPerHour = 60;

    function convertSecondsToHHMMSS(intSecondsToConvert)
    {
    var hours = convertHours(intSecondsToConvert);
    var minutes = getRemainingMinutes(intSecondsToConvert);
    minutes = (minutes == 60) ? "00" : minutes;
    var seconds = getRemainingSeconds(intSecondsToConvert);
    if (hours < 0)
    {
    hours+=24;
    minutes+=60;
    if (minutes < 10)
    {
    return hours+":"+"0"+minutes;
    }
    else
    {
    return hours+":"+minutes;
    }
    }
    if (minutes < 10)
    {
    return hours+":"+"0"+minutes;
    }
    else
    {
    return hours+":"+minutes;
    }
    }

    function convertHours(intSeconds)
    {
    var minutes = convertMinutes(intSeconds);
    var hours = Math.floor(minutes/minutesPerHour);
    return hours;
    }

    function convertMinutes(intSeconds)
    {
    return Math.floor(intSeconds/secondsPerMinute);
    }

    function getRemainingSeconds(intTotalSeconds)
    {
    return (intTotalSeconds%secondsPerMinute);
    }

    function getRemainingMinutes(intSeconds)
    {
    var intTotalMinutes = convertMinutes(intSeconds);
    return (intTotalMinutes%minutesPerHour);
    }

    function HMStoSec1(T)
    { // h:m:s
    var A = T.split(/\D+/) ;
    A[2]=0;
    return (A[0]*60 + +A[1])*60 + +A[2]
    }


    function cal_dif()
    {
    var time1 = HMStoSec1(document.getElementById("from").value);
    var time2 = HMStoSec1(document.getElementById("to").value);
    var diff = time2 - time1;
    document.getElementById("op").value=convertSeconds ToHHMMSS(diff);
    }

    </script>

    </head>
    <body>
    <input type="text" name="from" id="from" /><br />
    <input type="text" name="to" id="to" /><br />
    <input type="button" name="click" id="click" onclick=cal_dif() value="Difference" /><br />
    <input type="text" name="op" id="op" />
    </body>
    </html>


    Try this !!

Similar Threads

  1. How to calculate day difference for ASP
    By halfmoon in forum ASP Development
    Replies: 9
    Last Post: January 12th, 2007, 02:19 AM
  2. how to calculate Time Difference between two times
    By vikasp in forum ASP Development
    Replies: 8
    Last Post: March 11th, 2005, 09:56 AM
  3. How to calculate two difference in column
    By naiphan in forum SQL Development
    Replies: 1
    Last Post: October 13th, 2004, 08:07 PM
  4. How to calculate the difference between a range in Excel
    By rachelrekkab in forum HTML, JavaScript And CSS Help
    Replies: 0
    Last Post: August 12th, 2003, 02:07 AM
  5. Calculating the difference between two times and dates with ASP
    By Steve Schofield in forum ASP Development
    Replies: 1
    Last Post: November 26th, 2001, 10:44 AM

IMN logo majestic logo threadwatch logo seochat tools logo