How To Subtract Months From A Date In JavaScript

Subtract Months from a Date in JavaScript

In this article, I will show you how to subtract months from a Date in Javascript by using the getMonth() and setMonth() function. Let’s go into detail now.

Introduce setMonth() function

In Javascript, setMonth() function is used to set the month of the date object. It has a value from 0-11.

Syntax:

setMonth(monthValue, dayValue)

Parameters:

  • monthValue: An integer between 0 and 11, representing the months January through December (Required).
  • dayValue: An integer from 1 to 31, representing the day of the month (Optional).

Sample:

let date = new Date("July 25, 2022"); // 07/25/2022

// Set the month of the date object to June
date.setMonth(5);
console.log(date);

Output:

Sat Jun 25 2022 00:00:00 GMT+0700 (Indochina Time)

Subtract months from a date in JavaScript

Directly subtraction method

Sample:

let date = new Date("July 25, 2002"); // 07/25/2022

// Subtract 2 months
date.setMonth(date.getMonth() - 2);
console.log(date);

In this example, I used the getMonth() function to get the month value of the date object. Then subtract the number of months we want.

Output:

Sat May 25 2002 00:00:00 GMT+0700 (Indochina Time)

Reusable function

In the same way as above, we can create a reusable function as follows.

Sample:

function subMonth(date, month) {
	let newDate = new Date(date);
	newDate.setMonth(newDate.getMonth() - month);
	return String(newDate);
}

// Subtract 3 months from the 'date1' object by subMonth() function
let date1 = new Date("July 25, 2022");
console.log(subMonth(date1, 3));

// Subtract 1 month from the 'date2' object by subMonth() function
let date2 = new Date("2022-08-30");
console.log(subMonth(date2, 1));

// Subtract 2 months from current date by subMonth() function
let currentDate = new Date();
console.log(subMonth(currentDate, 2));

Output

Mon Apr 25 2022 00:00:00 GMT+0700 (Indochina Time)
Sat Jul 30 2022 07:00:00 GMT+0700 (Indochina Time)
Wed Aug 10 2022 21:23:19 GMT+0700 (Indochina Time)

Date.prototype.subMonth()

In this example, I add a function to Date.prototype.subMonth(), so we can use it on any Date object.

Sample:

Date.prototype.subMonth = function(months) {
	this.setMonth(this.getMonth() - months);
}

// Subtract 3 months from the 'date1' object have the format as mm dd,yyyy
let date1 = new Date("July 25, 2022");
date1.subMonth(3);
console.log(String(date1));

// Subtract 1 month from the 'date2' object have the format as yyyy-mm-dd
let date2 = new Date("2022-08-30");
date2.subMonth(1);
console.log(String(date2));

// Subtract 2 months from current date
let currentDate = new Date();
currentDate.subMonth(2);
console.log(String(currentDate));

Output

Mon Apr 25 2022 00:00:00 GMT+0700 (Indochina Time)
Sat Jul 30 2022 07:00:00 GMT+0700 (Indochina Time)
Wed Aug 10 2022 21:21:34 GMT+0700 (Indochina Time)

Summary

Above, I showed you how to subtract months from a Date in Javascript by using the setMonth() and getMonth() function. I hope they are helpful to you. To better understand the lesson’s content, practice rewriting today’s examples.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *