Explain Codes LogoExplain Codes Logo

Incrementing a date in JavaScript

javascript
date-manipulation
time-zone
daylight-saving-time
Anton ShumikhinbyAnton Shumikhin·Sep 29, 2024
TLDR

Getting straight to it, here's how you boost a Date by one day in JavaScript:

let tomorrow = new Date(); tomorrow.setDate(tomorrow.getDate() + 1);

This snippet takes care of month-end and leap years, automatically adjusting the date. Just replace 1 with any other number of days you wish to increment by.

Time Zone & Daylight Saving Time: An Unholy Union

Keep Calm and Use UTC

To dodge any pesky time zone related inconsistencies, choose to work with UTC dates:

let tomorrowUTC = new Date(Date.UTC(today.getUTCFullYear(), today.getUTCMonth(), today.getUTCDate())); tomorrowUTC.setUTCDate(tomorrowUTC.getUTCDate() + 1);

With this, you can say goodbye to woes of local time adjustments and daylight saving shifts.

DST: Not a Time Travel Movie

While juggling with local times, remain vigilant of the notorious Daylight Saving Time changes:

let today = new Date(); let tomorrow = new Date(today); tomorrow.setDate(tomorrow.getDate() + 1); if (tomorrow.getHours() !== today.getHours()) { // DST just time-traveled an hour!!! tomorrow.setHours(today.getHours()); }

This makes sure that hours remain constant even if DST meddles with the time.

What Could Go Wrong?

Superheroes: Month & Year Rollover

One league where JavaScript's Date shines is in handling rollovers:

let endOfYear = new Date('December 31, 2023'); endOfYear.setDate(endOfYear.getDate() + 1); // Happy New Year, folks! It's January 1, 2024

Leap Years: Not so Leap

Worried about leap years? JavaScript's got you:

let leapYear = new Date('February 28, 2024'); leapYear.setDate(leapYear.getDate() + 1); // Surprise! It's not March yet. It's February 29, 2024

Hello MomentJS

For a more uplifted API you could consider libraries like MomentJS:

// First, promise to include Moment let momentTomorrow = moment().add(1, 'days');

But remember, cloning instead of mutating is the golden rule:

let today = moment(); let tomorrow = moment(today).add(1, 'days'); // Cloning: Multiplying Happiness

Be aware, though, MomentJS carries its costs and for simpler tasks, native JavaScript could be lighter and speedier.

Code Shortcuts

Milliseconds: The Game Changer

Direct millisecond arithmetic to the rescue:

let tomorrow = new Date(new Date().getTime() + (1000 * 60 * 60 * 24)); // Because being straightforward is too mainstream

One-liners: Brevity at its Best

Conciseness, here we come:

let tomorrow = new Date(new Date().setDate(new Date().getDate() + 1)); // An affair of a Date with itself, times three

This one-liner has its charms, but turns a blind eye to time zones and DST changes.

Make it Stick: Saving the Incremented Date

When incrementing within loops or complex functions, always save the results:

function addDays(date, daysToAdd) { let result = new Date(date); result.setDate(result.getDate() + daysToAdd); return result; } // This one remembers what you did to the date