Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery - Get the time in HH:MM format between two dates

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...

like image 266
maaz Avatar asked Jan 17 '12 07:01

maaz


2 Answers

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/ )

like image 88
Diode Avatar answered Nov 20 '22 12:11

Diode


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());
});
like image 38
Sunil Raj Avatar answered Nov 20 '22 12:11

Sunil Raj