Skip to content

Latest commit

 

History

History
34 lines (26 loc) · 906 Bytes

create-future-and-past-dates-from-today.md

File metadata and controls

34 lines (26 loc) · 906 Bytes

Create Future And Past Dates From Today

JavaScript's built-in Date object can be frustrating to work with at times. It does, however, lend itself nicely to some date math. You have to familiarize yourself with some of the API and then it is a matter of addition and subtraction.

Here is today:

const today = new Date();
// Tue Dec 01 2020 ...

Let's make a copy of today and send it 30 days into the future:

const future = new Date(today);
future.setDate(future.getDate() + 30);
future
// Thu Dec 31 2020 ...

Or we could jump back a few years:

const past = new Date(today);
past.setFullYear(past.getFullYear() - 4);
past
// Thu Dec 01 2016 ...

source