<pre></pre>

// new Date('월/일/연')
const date1 = new Date('7/13/2010');
const date2 = new Date('12/15/2022');
const diffTime = Math.abs(date2 - date1);
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); 

write(
  // 밀리초 단위
  diffTime + " milliseconds",
  // 초 단위
  (diffTime / 1000) + " seconds",
  // 분 단위
  (diffTime / 1000 / 60) + " minutes",
  // 일 단위
  diffDays + " days",
  // 월 단위 (근사치)
  (diffDays / 30).toFixed(2) + " months(approx.)",
  // 연 단위 (근사치)
  (diffDays / 365).toFixed(2) + " years(approx.)"
);

// 연, 월 구하는 다른 방법
const date1Year = date1.getFullYear()
const date2Year = date2.getFullYear()

// 연 단위 (근사치) 구하기
const yearDiff = Math.abs(date1Year - date2Year)

// 월 단위 (근사치) 구하기
const monthDiff = yearDiff * 12 + Math.abs(date1.getMonth() - date2.getMonth())

write(
  monthDiff + " months(approx.)",
  yearDiff + " years(approx.)"
)

// console.log(dateToAddMonth.getMonth() - dateToMinusMonth.getMonth(), date1.getMonth() - date2.getMonth(), date2.getMonth() - date1.getMonth())

//////////////////////////////////////

function write() {
  for(let i = 0; i < arguments.length; i++) {
    document.querySelector("pre").textContent += `${arguments[i]}\n`
  }
}

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.