function getDay(d, num) {
const date = new Date(new Date(d).setDate(new Date(d).getDate() + num))
const year = date.getFullYear()
const month = (date.getMonth() + 1 + "").padStart(2, "0")
const day = (date.getDate() + "").padStart(2, "0")
return `${year}-${month}-${day}`
}
function lastYearDate(d) {
const lastYear = new Date(d).getFullYear() - 1
const lastYearIsLeap = (lastYear % 4 == 0 && lastYear % 100 != 0) || lastYear % 400 == 0
if (lastYearIsLeap) return getDay(d, -366)
return getDay(d, -365)
}
const date = "2021-2-10"
console.log(lastYearDate(date))
const date1 = "2021-3-1"
console.log(lastYearDate(date1))
const date2 = "2021-3-10"
console.log(lastYearDate(date2))
const date3 = "2020-2-10"
console.log(lastYearDate(date3))
const date4 = "2020-2-29"
console.log(lastYearDate(date4))
const date5 = "2020-3-10"
console.log(lastYearDate(date5))
const date6 = "2023-3-1"
console.log(lastYearDate(date6))
|