Blog / JavaScript

JavaScript: How to Add Days to Current Date

In one of the projects I decided to make additional UX features for quick selection of dates in the interface. On the side of the calendar there are buttons: select 3 days, 7 days and 30 days.

Here I share with you a trick on how to quickly add any number of days to the current date:

The global Date object in JavaScript has a huge set of functions for any date manipulation. Today we will try to use one of them to add e.g. a week to the current date, just use this trick:

const nextWeek = new Date()
nextWeek.setDate(new Date().getDate() + 7)
console.log(nextWeek)

The .setDate() function understands the context perfectly, and if the number of days selected exceeds the current month, it automatically changes it.