Below you can find a snippet to calculate the difference in days, hours, minutes, and seconds - between two dates.

```
const deadline = new Date("2050-01-01T00:00:00-06:00");
const now = new Date();
let delta = (deadline.getTime() - now.getTime()) / 1000;
const days = Math.floor(delta / 86400);
delta -= days * 86400;
const hours = Math.floor(delta / 3600) % 24;
delta -= hours * 3600;
const minutes = Math.floor(delta / 60) % 60;
delta -= minutes * 60;
const seconds = Math.floor(delta % 60);
```

## Explanation

First, we calculate the difference between two dates. `Date`

subtraction in Javascript, will yield a difference in milliseconds, therefor we divide by 1000. The rest - is just math.

`86400`

- is the number of seconds in a day (60x60x24)
`3600`

- is the number of seconds in an hour (60x60)

## Usage

You can use it to display a countdown towards a future date, for example by recalculating it every second, using `setInterval`

, and updating some HTML elements.

You can also use it to display how much time passed since a past date (a count up), but in that case, make sure yo wrap the `delta`

in `Math.abs`

, otherwise you will get negative numbers.