Explain Codes LogoExplain Codes Logo

How can I add 1 day to current date?

javascript
date-manipulation
time-zones
date-quirks
Alex KataevbyAlex KataevΒ·Feb 9, 2025
⚑TLDR

Quickly add a day to the current date with JavaScript:

let tomorrow = new Date(); tomorrow.setDate(tomorrow.getDate() + 1); // πŸ‘½ They say time travel is impossible. Well, we just did!

This code gets the current date using a Date object and increments the day by one. The Date object in JavaScript automatically adjusts the month and year as needed.

Dealing with time zones and daylight saving time

While manipulating dates and times, you need to be mindful of time zone differences and daylight saving time changes. Here's a common pitfall, adding 24 hours (in milliseconds) to Date.getTime():

let today = new Date(); let tomorrow = new Date(today.getTime() + (24 * 60 * 60 * 1000)); // πŸ‘Ά Baby steps are important, but not for time jumps!

This approach can lead to inaccuracies due to daylight saving time shifts. Thankfully, the Date object handles this for us when using setDate() and getDate().

Factoring in month-end scenarios

In date manipulation, watch out for the end of the month! For example, if you add a day to January 31st, you want February 1stβ€”not a mythical "January 32nd".

let endOfMonth = new Date('2023-01-31'); endOfMonth.setDate(endOfMonth.getDate() + 1); // πŸ‘€ Leap of faith? No, just a day! console.log(endOfMonth.toLocaleDateString()); // Outputs '2/1/2023' in MM/DD/YYYY format.

JavaScript's Date object keeps track of calendar limits for you!

Techniques for displaying dates

When you're ready to display your date, toLocaleDateString() comes to the rescue:

let formattedDate = tomorrow.toLocaleDateString('en-US'); // πŸ–οΈ For our friends at the beach, '4/6/2023'

Want to make friends in other countries? Use toLocaleDateString() with the appropriate locale. You can opt for a more universal standard like ISO 8601:

let isoDate = tomorrow.toISOString().substring(0, 10); // 🌍 For our friends across the globe, '2023-04-06'

Considering libraries for date manipulation

Though JavaScript provides powerful methods for date manipulation, complex scenarios might drive you towards libraries like moment.js or date-fns. They come with extensive features for parsing, validating, and formatting dates.

But remember, size matters! When moment.js feels too heavy, there are numerous lightweight champions offering the necessary functions for efficient date operations.

Working smart with JavaScript quirks

The JavaScript's Date object can be a tricky customer with its unexpected quirks. Consider leap years, time zones and using setDate(0) (which sets the date to the last day of the previous month).

let dateTest = new Date(); dateTest.setDate(dateTest.getDate() + 1); console.assert(dateTest.getDate() !== tomorrow.getDate(), "Oops! The dates turned out to be clones!"); // πŸ‘―β€β™‚οΈ

Always test your dates after manipulating them. Catching errors early saves... well, time!