아티클 목록

JS Date 시간 차이 구하기

JS Date 시간 차이 구하기

// 두 날짜 사이 시간 계산
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

참고

댓글