How to add days to Date using JavaScript? How to add days to current Date using JavaScript? Does JavaScript have a built-in function like .NET’s AddDay()?
Hey MattD,
Using the getTime method: This method gets the numeric value corresponding to the time for a specified date object. By adding the number of milliseconds in the desired number of days, a new date object is created with the updated date.
var currentDate = new Date(); var numberOfDaysToAdd = 5; var newDate = new Date(currentDate.getTime() + numberOfDaysToAdd * 24 * 60 * 60 * 1000);
Hey MattD,
Using the moment library: Moment.js is a popular JavaScript library for parsing, validating, manipulating, and formatting dates. It provides an add method that allows you to add a specified amount of time to a moment object, in this case, adding 5 days to the current date.
var currentDate = moment(); var numberOfDaysToAdd = 5; var newDate = currentDate.add(numberOfDaysToAdd, ‘days’);