
// UTC to KST
function getFormattedDate(dateString) {
const date = new Date(dateString);
return date.toLocaleString('sv');
}
// 약관 기한이 지났는지 아닌지 Boolean 타입으로 리턴
function checkPrivacies(today, time, term) {
const parsedDate = new Date(getFormattedDate(time));
const targetDate = new Date(parsedDate.setMonth(parsedDate.getMonth() + Number(term)));
const currentDate = new Date(today);
return getFormattedDate(targetDate) <= getFormattedDate(currentDate);
}
function solution(today, terms, privacies) {
const answer = [];
for (let i = 0; i < privacies.length; i++) {
const cur = privacies[i].split(' ');
for (let j = 0; j < terms.length; j++) {
const term = terms[j].split(' ');
if (cur[1] === term[0] && checkPrivacies(today, cur[0], term[1])) {
answer.push(i + 1);
}
}
}
return answer;
}
사실 Time, Date 연산은 기본이긴 하겠지만
구현에 많은 시간을 쓰고 싶지않아서 js Date객체로 연산을 대체했다.
Javscript의 Date객체를 쓰면 Terms에 주어진 month 연산이 쉬워진다.
까다로웠던 점이라면 주어지는 today가 KST이기 때문에 new Date()로 생성한 UTC Date 객체를 KST Date로 한번 더 파싱해줘야 한다는 점