-1

could you please tell me how to how to find difference of two times in javascript ? I tried to find but I am getting error in my fiddle here is my code

https://jsfiddle.net/fwz7mtyL/

var todaydate = new Date(); 
var datetime =  todaydate.getMonth()+1  + "-" 
                +todaydate.getDate() + "-"
                + todaydate.getFullYear() + " "  
                + todaydate.getHours() + ":"  
                + todaydate.getMinutes() + ":" 
                + todaydate.getSeconds();
var date2='8-6-2015 15:34:60";
var d1 = new Date(datetime);
var d2=new Date(date2);
alert(("Your Operation took  " + (d2.getTime() - d1.getTime()) + " milliseconds"))
alert(datetime)
user944513
  • 12,247
  • 49
  • 168
  • 318

1 Answers1

0

Instead of create your d2 this way.

var date2='2015-06-08T15:34:60';
var d2=new Date('2015','06'-1,'08','15','34','60');

You can directly pass these parameters at the constructor like this.

var d2=new Date('2015','06'-1,'08','15','34','60');

Look it works for me. Demo

Sapikelio
  • 2,594
  • 2
  • 16
  • 40