Question: I'm trying to get a difference between two dates in seconds. The logic would be like this :

* set an initial date which would be now;
* set a final date which would be the initial date plus some amount of seconds in future ( let's say 15 for instance )
* get the difference between those two ( the amount of seconds )

The reason why I'm doing it it with dates it's because the final date / time depends on some other variables and it's never the same ( it depends on how fast a user does something ) and I also store the initial date for other things.

I've been trying something like this : 

```
var _initial = new Date(),
    _initial = _initial.setDate(_initial.getDate()),
    _final = new Date(_initial);
    _final = _final.setDate(_final.getDate() + 15 / 1000 * 60);

var dif = Math.round((_final - _initial) / (1000 * 60));

```

The thing is that I never get the right difference. I tried dividing by `24 * 60` which would leave me with the seconds, but I never get it right. So what is it wrong with my logic ? I might be making some stupid mistake as it's quite late, but it bothers me that I cannot get it to work :)

Answer: 
You can use `new Date().getTime()` for getting timestamps. Then you can calculate the difference between end and start and finally transform the timestamp which is `ms` into `s`.

```
const start = new Date().getTime();
const end = new Date().getTime();

const diff = end - start;
const seconds = Math.floor(diff / 1000 % 60);

```
```
let startTime = new Date(timeStamp1);
let endTime = new Date(timeStamp2);

```

to get the difference between the dates in seconds ->

```
  let timeDiffInSeconds = Math.floor((endTime - startTime) / 1000);

```

but this porduces results in utc(for some reason that i dont know).
So you have to take account for timezone offset, which you can do so by adding

> 
> new Date().getTimezoneOffset();
> 
> 
> 

but this gives timezone offset in minutes, so you have to multiply it by 60 to get the difference in seconds.

```
  let timeDiffInSecondsWithTZOffset = timeDiffInSeconds + (new Date().getTimezoneOffset() * 60);

```

This will produce result which is correct according to any timezone & wont add/subtract hours based on your timezone relative to utc.