티스토리 뷰

우측: 책 / 좌측: 나

책과 결과 값의 차이가 커서 오탈자 봤더니 "month + 1"을 빼먹었더라;

(책은 재판하면서 수정놓고선 코드 수정은 안해놓음. 할말하않)

 

그리고 D-day는 당일부터 계산하기 때문에 -1을 해줘야 한다.

(예를 들어 100일인 경우, 1을 안빼주면 101일이 된다.)


내 코드

const startValue = prompt('사귀기 시작한 날짜를 입력해주세요.', "2018-03-23");
let startDay = new Date(startValue);
const toDay = new Date();
const dayMilli = 1000 * 60 * 60 * 24;
const accent = document.querySelector('#accent').firstElementChild;
const date100 = document.querySelector('#date100');
const date200 = document.querySelector('#date200');
const date365 = document.querySelector('#date365');
const date500 = document.querySelector('#date500');

function passedDay() {
    return Math.round((toDay.getTime() - startDay.getTime()) / dayMilli);
}

// function dDay(days) {
// 	const dayChange = new Date(startDay.getTime() + (days * dayMilli) - dayMilli);
// 	return `${dayChange.getFullYear()}년 ${dayChange.getMonth() + 1}월 ${dayChange.getDate()}일`;
// }

function dDay(days) {
    startDay = new Date(startValue);
    startDay.setDate(startDay.getDate() + days - 1);
    return `${startDay.getFullYear()}년 ${startDay.getMonth() + 1}월 ${startDay.getDate()}일`;
}

accent.textContent = `${passedDay()}일`;
date100.textContent = `${dDay(100)}`;
date200.textContent = `${dDay(200)}`;
date365.textContent = `${dDay(365)}`; 
date500.textContent = `${dDay(500)}`;

 


[공부 좌표]

Do It! 자바스크립트 기본편 P180 ~ 190

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함