[Vuejs]-Javascript date increase by a year and subtract a day

0👍

You forgot to call getFullYear, and instead of trying to format it yourself, you can use toLocaleDateString and pass in the locale you want the format to be in:

function formatDateNextYear(date) {
    if (!date) return null;

    let a = new Date(date);

    a.setDate(a.getDate() - 1);
    a.setFullYear(a.getFullYear() + 1);

    return a.toLocaleDateString("en-US");
}

console.log(formatDateNextYear(new Date("01/01/2022")));

Leave a comment