Here at Future Studio, we offer a selection of premium tutorials that require payment. You need to enroll for a semester in the Future Studio University.
The guest semester is an enrollment on a monthly basis. In case you’re enrolling now in the University, the next payment date is in one month. We also offer a semester enrollment which is valid for six months.
This tutorial shows you how to add one or more months to a date in JavaScript.
Node.js Series Overview
- Increase the Memory Limit for Your Process
- Why You Should Add “node” in Your Travis Config
- Create a PDF from HTML with Puppeteer and Handlebars
- Create Your Own Custom Error
- Extend Multiple Classes (Multi Inheritance)
- Get a File’s Created Date
- Get a File’s Last Modified/Updated Date
- Write a JSON Object to a File
- How to Create an Empty File
- Generate a Random Number in Range With JavaScript/Node.js
- How to Merge Objects
- Retrieve a Request’s IP Address in Node.js
- Detect the Node.js Version in a Running Process or App
- Install Dependencies for a Specific Package
- String Replace All Appearances
- Remove All Whitespace From a String in JavaScript
- Generate a Random String in Node.js or JavaScript
- Remove Extra Spaces From a String in JavaScript or Node.js
- Remove Numbers From a String in JavaScript or Node.js
- Get the Part Before a Character in a String in JavaScript or Node.js
- Get the Part After a Character in a String in JavaScript or Node.js
- How to Check if a Value is a String in JavaScript or Node.js
- Check If a String Includes All Strings in JavaScript/Node.js/TypeScript
- Get Number of Seconds Since Epoch in JavaScript
- Get Tomorrow’s Date in JavaScript
- Increase a Date in JavaScript by One Week
- Add Seconds to a Date in Node.js and JavaScript
- Add Month(s) to a Date in JavaScript or Node.js
- How to Run an Asynchronous Function in Array.map()
- How to Reset and Empty an Array
- for…of vs. for…in Loops
- Clone/Copy an Array in JavaScript and Node.js
- Get an Array With Unique Values (Delete Duplicates)
- Sort an Array of Integers in JavaScript and Node.js
- Sort a Boolean Array in JavaScript, TypeScript, or Node.js
- Check If an Array Contains a Given Value in JavaScript or Node.js
- Add an Item to the Beginning of an Array in JavaScript or Node.js (Coming soon)
- Append an Item at the End of an Array in JavaScript or Node.js (Coming soon)
- Callback and Promise Support in your Node.js Modules
- Run Async Functions/Promises in Sequence
- Run Async Functions/Promises in Parallel
- Run Async Functions in Batches
- How to Fix “Promise resolver undefined is not a function” in Node.js or JavaScript
- Detect if Value Is a Promise in Node.js and JavaScript
- How to Check if an Object is Empty in JavaScript or Node.js
- How to CamelCase Keys of an Object in JavaScript or Node.js (Coming soon)
Add Month(s) to a Date in JavaScript
JavaScript comes with a Date
class providing useful date handling. You can customize a date instance by adding the units you need. A date instance comes with .setX
methods to customize it.
In this case, you want to add a given number of months to a date. Add the given amount of months to a date instance using date.setMonth(currentMonth + delay)
.
/**
* Returns the date with added `months` of delay.
*
* @param {Number} months - the delay in months
*
* @returns {Date}
*/
function dateWithMonthsDelay (months) {
const date = new Date()
date.setMonth(date.getMonth() + months)
return date
}
Notice that date.setMonth
directly changes the date
instance.
A nice benefit of using this built-in method is that JavaScript takes care of switching the unit. For example, JavaScript also adjusts the years of your date instance if adding the months would exceed December. Sweet!
Mentioned Resources
- Your shortcut to the Future Studio University 🥳