6

I am getting the values from two text fields as date

var start_actual_time = $("#startPoint_complete_date").val();
var end_actual_time = $("#endPoint_complete_date").val();

which gives value

start_actual_time  =  01/17/2012 11:20
end_actual_time    =  01/18/2012 12:20

now i want to find out the duration in HH:MM format between these two dates (which is 25:00 in this case) how can i do it...

maaz
  • 3,534
  • 19
  • 61
  • 100
  • Check this link http://stackoverflow.com/questions/2609513/jquery-calculate-day-difference-in-2-date-textboxes – reshma k Jan 17 '12 at 08:03
  • look [here][1] and [here][2] [1]: http://stackoverflow.com/questions/327429/whats-the-best-way-to-calculate-date-difference-in-javascript [2]: http://stackoverflow.com/questions/175554/how-to-convert-milliseconds-into-human-readable-form – A.B.Cade Jan 17 '12 at 08:04
  • don't you need days too? – redmoon7777 Jan 17 '12 at 08:04
  • 1
    here is a good example http://www.javascriptsource.com/math-related/date-difference.html – A.B.Cade Jan 17 '12 at 08:10

6 Answers6

13
var start_actual_time  =  "01/17/2012 11:20";
var end_actual_time    =  "01/18/2012 12:25";

start_actual_time = new Date(start_actual_time);
end_actual_time = new Date(end_actual_time);

var diff = end_actual_time - start_actual_time;

var diffSeconds = diff/1000;
var HH = Math.floor(diffSeconds/3600);
var MM = Math.floor(diffSeconds%3600)/60;

var formatted = ((HH < 10)?("0" + HH):HH) + ":" + ((MM < 10)?("0" + MM):MM)
alert(formatted);

See demo : http://jsfiddle.net/diode/nuv7t/5/ ( change mootools in jsfiddle or open http://jsfiddle.net/nuv7t/564/ )

Diode
  • 24,570
  • 8
  • 40
  • 51
1

Working example:

gives alert message as 6:30

$(function(){
    var startdate=new Date("01/17/2012 11:20");
    var enddate=new Date("01/18/2012 12:20");
    var diff = new Date(enddate - startdate);   
    alert(diff.getHours()+":"+diff.getMinutes());
});
Sunil Raj
  • 460
  • 5
  • 20
0

First, I recommend this: http://www.mattkruse.com/javascript/date/ to convert the string to a date object, but you can convert it any way you want. Once converted, you can do this:

var difference_datetime = end_datetime.getTime() - start_datetime.getTime()

The getTime() function gets the number of milliseconds since 1970/01/01. Now you have the number of milliseconds of difference, and you can do whatever you want to convert to higher numbers (eg. divide by 1000 for seconds, divide that by 60 for minutes, divide that by 60 for hours, etc.)

Andrew Jackman
  • 13,781
  • 7
  • 35
  • 44
0

I did something simular on a blog to see how long im together with my gf :P http://daystogether.blogspot.com/ and this is how I did it:

// *****Set the unit values in milliseconds.*****

var msecPerMinute = 1000 * 60;
var msecPerHour = msecPerMinute * 60;
var msecPerDay = msecPerHour * 24;

// *****Setting dates*****

var today = new Date();
var startDate = new Date('10/27/2011 11:00:00');

// *****Calculate time elapsed, in MS*****
var interval = today.getTime() - startDate.getTime();

var days = Math.floor(interval / msecPerDay );
interval = interval - (days * msecPerDay );

var weeks = 0;
while(days >= 7)
{
days = days - 7;
weeks = weeks + 1;
}

var months = 0;
while(weeks >= 4)
{
weeks = weeks - 4;
months = months + 1;
}


// Calculate the hours, minutes, and seconds.
var hours = Math.floor(interval / msecPerHour );
interval = interval - (hours * msecPerHour );

var minutes = Math.floor(interval / msecPerMinute );
interval = interval - (minutes * msecPerMinute );

var seconds = Math.floor(interval / 1000 );

BTW this is just javascript, no jquery ;)

Teun Pronk
  • 1,367
  • 12
  • 24
0

here you go:

function get_time_difference(start,end)
{               
    start = new Date(start);
    end = new Date(end);
    var diff = end.getTime() - start.getTime();                 
    var time_difference = new Object();

    time_difference.hours = Math.floor(diff/1000/60/60);        
    diff -= time_difference.hours*1000*60*60;
    if(time_difference.hours < 10) time_difference.hours = "0" + time_difference.hours;

    time_difference.minutes = Math.floor(diff/1000/60);     
    diff -= time_difference.minutes*1000*60;    
    if(time_difference.minutes < 10) time_difference.minutes = "0" + time_difference.minutes;                                  

    return time_difference;              
}

var time_difference = get_time_difference("01/17/2012 11:20", "01/18/2012 12:20");

alert(time_difference.hours + ":" + time_difference.minutes);
redmoon7777
  • 4,498
  • 1
  • 24
  • 26
0
start_date.setTime(Date.parse(start_actual_time));
end_date.setTime(Date.parse(end_actual_time));

//for check
start_date.toUTCString()
end_date.toUTCString()

/**
 * Different in seconds
 * @var diff int
 */

var diff = (end_date.getTime()-start_date.getTime())/1000;
devzorg
  • 105
  • 7