---
title: "JS Date 시간 차이 구하기"
date: "2022-05-21"
tags: ["TIL","JS"]
status: archived
verified: false
---

## JS Date 시간 차이 구하기

```js
// 두 날짜 사이 시간 계산
const start = new Date();
const end = new Date();

// ms -> seconds
const gapSeconds = (end.getTime() - start.getTime()) / 1000;

// hh:mm 형태 문자열 구하기
new Date(gapSeconds * 1000).toISOString().substring(11, 16); // hh:mm
new Date(gapSeconds * 1000).toISOString().substring(14, 19); // hh:mm:ss
```

### 참고

-   [stackoverflow - Get time difference between two dates in seconds](https://stackoverflow.com/questions/13894632/get-time-difference-between-two-dates-in-seconds/13894670#13894670)
-   [stackoverflow - Convert seconds to HH-MM-SS with JavaScript?](https://stackoverflow.com/questions/1322732/convert-seconds-to-hh-mm-ss-with-javascript/1322771#1322771)
