0

I am using telerik controls and i want difference between two datetime variables how can i calculate.

var x=9/1/2012 10:20:00 AM;
var y=9/1/2012 09:00:00 AM;

I want difference between two times throw javascript in to another variable as number.

Cœur
  • 37,241
  • 25
  • 195
  • 267
vishali
  • 95
  • 7
  • 21
  • 2
    C# or JavaScript? Please choose *one* .. also, please use *valid code*. (That is invalid syntax in both C# *and* JavaScript.) –  Aug 07 '12 at 06:36

3 Answers3

2

Create two Date type variable and then calculate the difference using - operator, you will get difference in milliseconds.

var first = new Date(2012,8,1,10,20,0,0); 
var second = new Date(2012, 9, 1, 09, 00, 0, 0); 
var difference = (second - first); // difference in milliseconds

Here is working demo

Habib
  • 219,104
  • 29
  • 407
  • 436
2

You should try something on these lines

var a = new Date('9/1/2012 10:20:00 AM');
var b = new Date('9/1/2012 09:00:00 AM');
// a - b  this should give you the diff
var diff = a - b;
alert(diff/1000); // this should be value in seconds i.e 4800
V4Vendetta
  • 37,194
  • 9
  • 78
  • 82
0

See this

Basically, put those two dates into Date objects, and subtract one from the other. That will give you the difference in milliseconds and you can work from there to get larger time segments.

Community
  • 1
  • 1
Adrian
  • 2,825
  • 1
  • 22
  • 32