Add Days to a Date in JavaScript

Adding days to a date in JavaScript is fairly easy using the Date object.

You can update a date using the .setDate() method.

Here's how:

// Assume you have a date but let's create one
const myDate = new Date();
const daysToAdd = 5;

// Now if we want to alter it we make sure it's in the Date object:
const result = new Date(myDate)
result.setDate(result.getDate() + daysToAdd);
// Now, if you use the result it'll be 5 days from now

This code just creates a new date, adds the specified number of days, and automatically handles month and year transitions.

EZ!

JavaScript
Avatar for Niall Maher

Written by Niall Maher

Founder of Codú - The web developer community! I've worked in nearly every corner of technology businesses: Lead Developer, Software Architect, Product Manager, CTO, and now happily a Founder.

Loading

Fetching comments

Hey! 👋

Got something to say?

or to leave a comment.