Files
30-seconds-of-code/snippets/js/s/date-difference-in-seconds.md
Angelos Chalaris 9d032ce05e Rename js snippets
2023-05-19 20:23:47 +03:00

25 lines
574 B
Markdown

---
title: Date difference in seconds
type: snippet
language: javascript
tags: [date]
cover: laptop-journey
dateModified: 2021-04-24T12:39:48+03:00
---
Calculates the difference (in seconds) between two dates.
- Subtract the two `Date` objects and divide by the number of milliseconds in a second to get the difference (in seconds) between them.
```js
const getSecondsDiffBetweenDates = (dateInitial, dateFinal) =>
(dateFinal - dateInitial) / 1000;
```
```js
getSecondsDiffBetweenDates(
new Date('2020-12-24 00:00:15'),
new Date('2020-12-24 00:00:17')
); // 2
```