Home >Web Front-end >JS Tutorial >How Can I Add Days to a Date in JavaScript?
Many programming languages, such as C#, offer a convenient method like AddDay() to manipulate dates. JavaScript, however, lacks a similar built-in function. This article will provide a custom solution to add days to a Date object in JavaScript.
To fill this gap, you can create your own function using the following code snippet:
Date.prototype.addDays = function(days) { var date = new Date(this.valueOf()); date.setDate(date.getDate() + days); return date; }
To use this custom function, simply call the addDays() method on a Date object. For example:
var date = new Date(); console.log(date.addDays(5));
This will output a new Date object representing the date 5 days after the original date.
The above is the detailed content of How Can I Add Days to a Date in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!