-5

i have this code in php(calculate the difference between "NOW" to another date - in hour,second and minute)

$date_now=date("Y-m-d");
$hour_now=date("G:i:s",time() + 3600); // without +3600, i get one hour back from the real time 

$date_time_now = new dateTime($date_now." ".$hour_now);
$date_time_start = new dateTime("2018-06-14"." "."11:46:24");

$diff=$date_time_now->diff($date_time_start);

$hour= $diff->h;
$seconds= $diff->s;
$minutes= $diff ->i;

how can i do this calculate in javascript / Jquery ?

Novice
  • 3
  • 1
  • 8
    Possible duplicate of [Difference between dates in JavaScript](https://stackoverflow.com/questions/1968167/difference-between-dates-in-javascript) – Herohtar Jun 14 '18 at 18:17
  • by momentjs https://stackoverflow.com/questions/25150570/get-hours-difference-between-two-dates-in-moment-js – Metalik Jun 14 '18 at 18:18
  • https://stackoverflow.com/questions/11883768/jquery-time-difference-in-hours-from-two-fields – Munish Chechi Jun 14 '18 at 18:19

1 Answers1

1

You can use Dates in javascript.

To get the now-Date you simply do Date.now(). To get the date from a specific time you can look into the wonderful MDN docs which says:

new Date(year, monthIndex [, day [, hour [, minutes [, seconds [, milliseconds]]]]]);

Now you can simply substract both dates and what you get are the difference in milliseconds:

Date.now() - new Date(1980, 1, 20) // 20th february 1980
Fuzzyma
  • 7,619
  • 6
  • 28
  • 60
  • yes, but my problem is to convert the milliseconds to hour,minutes and seconds , so that , for example , 121000 milliseconds will be : 2 minutes and 1 seconds – Novice Jun 14 '18 at 18:25
  • well - do the maths. You are a programmer. 1sec = 1000msec; 1min = 60sec; 1h = 60min; 1d = 24h; I am pretty sure you know that! – Fuzzyma Jun 14 '18 at 18:29