0

In my app I have two date strings:

Say that they are: date1 = "2014-03-14 18:25:15"; date2 = "2014-03-14 16:26:15";

I get these date strings based on two events that the customer selects. Now I need to show the difference between these two strings in HH:MM:DD format.

What I am currently doing is, posting to PHP using AJAX and then doing the calcuation in the server:

$rDate = new DateTime($date1);
$tDate = new DateTime($date2);
$interval = date_diff($rDate,$tDate);
echo $interval->format('%h:%i:%s');

Then in the AJAX response handler I print it to a div

My problem is that server trip is just too much an overkill for this. How can I achieve the same thing from browser itself? (Javascript/Jquery/MomentJS)...

Any help is greatly appreciated!

Undefined Variable
  • 4,196
  • 10
  • 40
  • 69
  • 2
    maybe take a look at this posting http://stackoverflow.com/questions/2024198/how-many-seconds-between-two-dates – donald123 Jun 26 '15 at 07:28

1 Answers1

0

I'd suggest looking into Moment.js, which is a very well featured date handling library for Javascript.

Here's the relevant manual link for Moment.js for what you're wanting to do: http://momentjs.com/docs/#/displaying/difference/

Hope that helps.

Simba
  • 4,952
  • 3
  • 19
  • 29