0

how to get the duration time in date that has a time.

e.g start date and time: 2017-07-27 10:31 end date and time: 2017-07-28 15:11

(start date and time) - (end date and time) = total.

after getting the differences it should be convert into time.

Low Lee
  • 1
  • 1
  • You firstly need to parse the strings to dates, so [*Why does Date.parse give incorrect results*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) may apply. Then simply subtract one Date from the other to get the difference in milliseconds and convert to whatever units suit (days, hours, minutes, seconds, etc.) – RobG Aug 08 '17 at 09:31

2 Answers2

0

You have to use this :

const total = a.getTime() - b.getTime()
ZAhmed
  • 1,232
  • 8
  • 15
  • Perhaps if the OP had Date objects that would work. – RobG Aug 08 '17 at 09:31
  • if he has strings , he can use new date object with this string then do my solution , what you think ? – ZAhmed Aug 08 '17 at 09:32
  • Not a good idea. Parsing strings with the built-in parser is not reliable, `new Date("2017-07-27 10:31")` returns an invalid date in Safari. – RobG Aug 08 '17 at 09:36
0

You can do this by taking the start time from the end time

var startDate = new Date("2017-07-27 10:31");
var endDate = new Date("2017-07-28 15:11");

var durationSeconds = (endDate - startDate) / 1000;

From the seconds you can work out the minutes, hours etc.

Sean
  • 1,444
  • 1
  • 11
  • 21
  • Please do not suggest parsing strings with the built–in parser, see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results). – RobG Aug 08 '17 at 09:32