1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function calculateDDay(targetDateStr) {
// 현재 날짜와 시간을 가져옵니다.
const now = new Date();
// 현재 날짜에 시간을 00:00:00으로 설정하여 날짜만 비교하게 합니다.
now.setHours(0, 0, 0, 0);

// 목표 날짜를 Date 객체로 변환합니다.
const targetDate = new Date(targetDateStr);
targetDate.setHours(0, 0, 0, 0);

// 두 날짜의 차이를 밀리초 단위로 계산한 후 일(day) 단위로 변환합니다.
const diffMilliseconds = targetDate.getTime() - now.getTime();
const diffDays = diffMilliseconds / (1000 * 60 * 60 * 24);

return Math.ceil(diffDays); // 올림처리하여 D-Day를 계산합니다.
}

이번에 사용권 만료 기간을 D-Day로 표시하기 위해 JavaScript로 D-Day를 계산해주는 함수를 구현해보았다.