1

I am trying to calculate the time duration of a tasks, that I get from an ajax response.

Following are my table values:

 Jobid   techid, techtype,    notes,                            starttime,             stoptime
    1      1     Brakes       Break disc needed to be changed   2020-07-16 13:00:00   2020-07-16 13:40:00
    1      2     Oil Change   Replaced oil                      2020-07-17 08:00:00   2020-07-17 09:00:00
    1      3     Cleaning     Cleaned the vehicle               2020-07-17 10:00:00   2020-07-17 10:30:00

On my ajax response, in the above case, I am getting 3 objects each having the start time, and stop time. I want to calculate total time spent in hours and minutes.

Is there an easy way to calculate the total duration?

edcoder
  • 503
  • 1
  • 5
  • 19
  • What is the problem ? – sonic Jul 17 '20 at 13:14
  • This is essentially about subtracting two [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)s, which you do just like as if they are numbers. That'll get you the difference in milliseconds. Then it's basic calculus. (note that your question title is really misleading because it sounds like you want to measure the time until the ajax response finishes, although ajax is in fact completely irrelevant to the question) –  Jul 17 '20 at 13:14
  • Is this an ajax question? – fedesc Jul 17 '20 at 13:15
  • 1
    Does this answer your question? [Difference between dates in JavaScript](https://stackoverflow.com/questions/1968167/difference-between-dates-in-javascript) –  Jul 17 '20 at 13:16
  • Hi Chris, sorry I posted this question based on how to calculate the duration given say there are 10 rows start times and 10 stop times, will I need to subtract each of them and add to the duration. Or if there was an easier way to do it. – edcoder Jul 17 '20 at 14:51

3 Answers3

2

With a string like 2020-07-16 13:00:00 you can construct a JS Date and get the milliseconds since the UNIX epoch with getTime() like so

new Date('2020-07-16 13:00:00').getTime()

Or, if you prefer, as pointed out by @Yousaf in the comments you can actually just use the - operator with Dates directly and get the millisecond difference

// resolves to 3600000, or 1 hour in milliseconds
new Date('2020-07-16 13:00:00') - new Date('2020-07-16 12:00:00') 

Using that, you can get the difference in milliseconds between any two dates, and convert that to hours / minutes / whatever with straightforward arithmetic.

davnicwil
  • 28,487
  • 16
  • 107
  • 123
  • 2
    there's about five duplicates for this on SO, let's not make it six, I guess? –  Jul 17 '20 at 13:18
  • 2
    I take your point, but I generally answer questions here just to help people with concrete problems, like this one. There's probably some overlap with other questions, sure, but by nature of the question the OP may not know what to google for to find them. I'm just trying to help someone out :-) – davnicwil Jul 17 '20 at 13:23
  • You could just subtract 2 `Date` instances and the result will be difference in milliseconds, no need to call `.getTime()` method. – Yousaf Jul 17 '20 at 13:26
  • 1
    @Yousaf Nice! Didn't know that – davnicwil Jul 17 '20 at 13:27
  • And this is definitely appreciated, but it adds clutter to the site, also it'll help OP more in the long run if they learn how to help themselves (by learning how to search for existing answers, or even better: learning how to figure out the solution on their own, especially given how trivially simple this problem is, even for a newbie) –  Jul 17 '20 at 13:31
1

You can simply use Date to construct a date and then minus the start time from the end time.

Here I use getTime to get the millisecond difference, divide by 1000 to get seconds and divide by 60 to get minutes.

You could also use getMonth and such if you have bigger differences.

const starttime = '2020-07-16 13:00:00'   
const stoptime = '2020-07-16 13:40:00'

const duration = new Date(stoptime) - new Date(starttime)
console.log(duration / 1000 / 60)
Cjmarkham
  • 9,484
  • 5
  • 48
  • 81
  • 1
    there's about five duplicates for this on SO, let's not make it six, I guess? –  Jul 17 '20 at 13:18
  • those duplicates are very old and the one you posted works with integer dates not strings. – Cjmarkham Jul 17 '20 at 13:19
  • there's no need to convert `duration` to `Date`, `(duration / 1000) / 60` will give you the duration in minutes – Yousaf Jul 17 '20 at 13:22
  • Doesn't matter; the duplicate shows how to construct Date objects and that you can simply subtract them; this isn't a newbie tutorial site, some work by the OP is required, always. –  Jul 17 '20 at 13:32
-1

[UPDATE]

I think you can check this answer, but basically you should convert each date to js Date, get the milliseconds and just calculate endtime - startime.

const timelapse = new Date(endtime).getTime() - new Date(startime).getTime();

From there, you transform that in the unit you need (e.g: seconds = milliseconds/1000);

Sorry, my bad for writing fast.

Cristian
  • 34
  • 1
  • 6