Explain Codes LogoExplain Codes Logo

How to add a number of days to today's date?

javascript
date-manipulation
javascript-8
functions
Anton ShumikhinbyAnton Shumikhin·Nov 13, 2024
TLDR
const addDaysToDate = (daysToAdd) => new Date(Date.now() + daysToAdd * 86400000).toDateString(); console.log(addDaysToDate(10)); // "10 days later date"

This addDaysToDate function comes to rescue when you want to leap through time. It calculates milliseconds for a given number of days and adds them to the current time stamp provided by Date.now(). A formatted string corresponding to the new date is your final reward.

Understanding our time machine

Dealing with dates can sometimes feel like stepping on a Lego, particularly when you throw in timezones and daylight saving time into the mix. Remember, this function is for those living in a UTC world. If your application demands timezone-specific dates, then get ready to do some tweaking!

Popping the browser bubbles

This function is a citizen of the world, or so it believes. It has been spotted working comfortably across all modern browsers. But, should your code need to time travel back to older browsers, don't forget your testing kit!

Playing on the edge...cases

Keep an eye out for the shy guys – leap years and transitions in and out of daylight saving time. These folks can't resist throwing a bit of chaos into your date calculations.

Build once, reuse anywhere

Love recycling? So do we! Turn the function into a reusable method that adds days to any date:

// Why should today have all the fun? Date.prototype.addDays = function(days) { let date = new Date(this.valueOf()); date.setDate(date.getDate() + parseInt(days, 10)); // Always use protection: parseInt() return date; }; let date = new Date(); // Feeling 10 days younger? Subtract days, just like your age! console.log(date.addDays(10));

Libraries? We don't need no stinking libraries!

Yes, libraries like Moment.js can make date manipulation smoother than a fresh jar of Skippy. But trust me, friends don't let friends skip understanding core JavaScript, especially for something as doable as time calculations.

moment of fame

If you think Moment.js is your cup of tea, prepare a similar solution like this:

const moment = require('moment'); // Hey Moment.js, fast forward 10 days, would ya? console.log(moment().add(10, 'days').toDate());

Back to the future

Play the time tape backward by using negative numbers:

console.log(addDaysToDate(-10)); // "10 days ago date"

Tailoring your time machine

Add the addDays() method or addDaysToDate to your developer's toolbox and customize them to fit numerous situations. They can help with reminders, future predictions, or even historical reconstructions.

Pitfalls: Here be dragons

Always test after time traveling! Even when you think that you've covered all the edge cases like leap years or DST changes, it can be easy to get lost in time. So, if you're emulating Bill and Ted, make sure to validate your excellent adventures!