Start time is :- 02/18/2014 11:00 End time is :-02/27/2014 09:33 I want to calculate the difference between 2 dates using jquery so that I can add validation that user must keep the difference of 24 hour (1day) while selecting the date.
Asked
Active
Viewed 303 times
0
-
Okay, good luck. Come back with some code when you got problems. – elclanrs Feb 18 '14 at 04:18
-
how can I calculate the difference using jquery – Piya Sharma Feb 18 '14 at 04:20
-
See if this helps http://stackoverflow.com/questions/3224834/get-difference-between-2-dates-in-javascript – elclanrs Feb 18 '14 at 04:22
2 Answers
0
this is the jquery
var start = new Date("2014-02-18");
var end = new Date();
var diff = new Date(end - start);
var days = diff/1000/60/60/24
//alert(days);
if(days<=1){
alert("less then 1 day");
}
else{
alert("more then 1 day");
}
this is the jsfiddle for it

Standin.Wolf
- 1,224
- 1
- 10
- 32
0
var start_actual_time = "02/18/2014 11:00";
var end_actual_time = "02/27/2014 09:33";
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);

Nidheesh
- 4,390
- 29
- 87
- 150