Computer >> 컴퓨터 >  >> 프로그램 작성 >> JavaScript

JavaScript로 어제 날짜를 얻는 방법

JavaScript로 어제 날짜를 얻으려면 현재 날짜를 가져온 다음 다음과 같이 하루를 빼면 됩니다.

const today = new Date()
const yesterday = new Date(today)

yesterday.setDate(yesterday.getDate() - 1)

today.toDateString()
yesterday.toDateString()

이제 어제 날짜에 콘솔에서 로그아웃해 보세요.

console.log(yesterday.toDateString())

멋지고 간단합니다!