Add or subtract JavaScript date


This post goes over how to add or subtract a JavaScript date.

Add

To add 1 day to today (now):

const date = new Date();
date.setDate(date.getDate() + 1);

To add 2 months to tomorrow:

const date = new Date();
date.setDate(date.getDate() + 1);
date.setMonth(date.getMonth() + 2);

To add 42 days to 2020-04-20:

const date = new Date('2020-04-20');
date.setDate(date.getDate() + 42);

Subtract

To subtract 1 day from today (now):

const date = new Date();
date.setDate(date.getDate() - 1);

To subtract 2 months from yesterday:

const date = new Date();
date.setDate(date.getDate() - 1);
date.setMonth(date.getMonth() - 2);

To subtract 42 days from 2020-04-20:

const date = new Date('2020-04-02');
date.setDate(date.getDate() - 42);


Please support this site and join our Discord!