ASP.NET - how to find difference between two datetime in javascript

Asked By mani on 04-May-12 03:12 AM
hi i have two data as

start 22/10/2012 10:20

and

end 22/10/2012 20:00

in datatime format..

how can i get difference between these date using javascript..
Chintan Vaghela replied to mani on 04-May-12 04:13 AM

Hello,

First you need to split Date and convert into mm/dd/yyyy hh:mm format

 

After then calculate two date difference (in days, hours, minute, sec)

 

Your Javascript function as become following way

 

function GetDateDifference() {

 

        var startDate = "20/10/2012 10:20".split(/\//);

        var endDate = "22/10/2012 20:15".split(/\//);

 

 

        var sdt = new Date([startDate[1], startDate[0], startDate[2]].join('/'));

        var edt = new Date([endDate[1], endDate[0], endDate[2]].join('/'));

 

       

        var difference = edt.getTime() - sdt.getTime();

 

        var daysDifference = Math.floor(difference / 1000 / 60 / 60 / 24);

        difference -= daysDifference * 1000 * 60 * 60 * 24

 

        var hoursDifference = Math.floor(difference / 1000 / 60 / 60);

        difference -= hoursDifference * 1000 * 60 * 60

 

        var minutesDifference = Math.floor(difference / 1000 / 60);

        difference -= minutesDifference * 1000 * 60

 

        var secondsDifference = Math.floor(difference / 1000);

 

        var Remain = 'difference = ' + daysDifference + ' day/s  ' + hoursDifference + ' hour/s  ' + minutesDifference + ' minute/s  ' + secondsDifference + ' second/s ';

 

        alert(Remain);

 

 

 

 

 

      

      }

 

Hope this helpful!

Thanks