How To Add Years to a Date in JavaScript with date-fns

node v18.11.0
version: 1.0.0
endpointsharetweet
// import isn't supported in Node v18.11.0 otherwise it would be // import { addYear } from 'date-fns' const dateFns = require( "date-fns"); const { addYears } = dateFns; // 31th of January 2023 const date = new Date(2023, 0, 31); const leapYear = new Date(2024, 1, 29); console.log(date); console.log('Add 1 year: ', addYears(date, 1)); console.log('Add 1 year to leap year: ', addYears(leapYear, 1)); console.log('Add 3 year: ', addYears(date, 3)); console.log('Add 9 year: ', addYears(date, 9)); console.log(date);
Loading…

no comments

    sign in to comment