JavaScript Subtract Days from Date Object
Last Updated :
20 Jun, 2023
Given a date and the task is to subtract days from the date using JavaScript. To subtract days from date, some methods are used which are described below:
JavaScript getDate() Method: This method returns the day of the month (from 1 to 31) for the defined date.
Syntax:
Date.getDate()
Parameters: This method does not accept any parameters.
Return value: It returns a number from 1 to 31, representing the day of the month.
JavaScript setDate() Method: This method sets the day of month-to-date object.
Syntax:
Date.setDate(day)
Parameters:
- day: It is required parameter. It specifies the integer representing the day of the month. Values expected values are 1-31, but other values are also allowed.
- 0 will result in the last day of the previous month.
- -1 will result in the day before the last day of the previous month.
- If the month has 31 days, 32 will result in the first day of the next month.
- If the month has 30 days, 32 will result in the second day of the next month.
Return value: This method return the number of milliseconds between the date object and midnight of January 1, 1970
JavaScript getTime() Method: This method returns the number of milliseconds between midnight of January 1, 1970, and the specified date.
Syntax:
Date.getTime()
Parameters: This method does not accept any parameters.
Return value: It returns a number, representing the number of milliseconds since midnight on January 1, 1970.
JavaScript setTime() Method: This method sets the date and time by adding/subtracting a defined number of milliseconds to/from midnight January 1, 1970.
Syntax:
Date.setTime(millisec)
Parameters:
- millisec: It is required parameter. It specifies the number of milliseconds to be added/subtracted, midnight January 1, 1970.
Return value: It represents the number of milliseconds between the date object and midnight January 1, 1970
Example 1: This example subtract 4 days from the variable today by using setTime() and getTime() methods.
JavaScript
let today = new Date();
console.log("Today's date = " + today);
Date.prototype.subtractDays = function (d) {
this.setTime(this.getTime()
- (d * 24 * 60 * 60 * 1000));
return this;
}
let a = new Date();
a.subtractDays(4);
console.log(a);
Output:
Today's date = Tue Jun 13 2023 22:38:28 GMT+0530 (India Standard Time)
Date Fri Jun 09 2023 22:38:28 GMT+0530 (India Standard Time)
Example 2: This example subtract 365 days from the variable today by using setDate() and getDate() methods.
JavaScript
let today = new Date();
console.log("Today's date = " + today);
Date.prototype.subtractDays = function (d) {
this.setDate(this.getDate() - d);
return this;
}
let a = new Date();
a.subtractDays(365);
console.log(a);
Output:
Today's date = Tue Jun 13 2023 22:41:00 GMT+0530 (India Standard Time)
Date Mon Jun 13 2022 22:41:00 GMT+0530 (India Standard Time)
Similar Reads
How to Add Days to Date in JavaScript? Adding days to a date in JavaScript is a common task when working with dates and times. This involves manipulating the Date object to calculate a future or past date by adding a specific number of days. This is useful for tasks like scheduling events, and deadlines, or simply adjusting dates dynamic
2 min read
JavaScript Date UTC() Method In JavaScript, the Date.UTC() method is used to create a date object representing a specified date and time in UTC (Coordinated Universal Time). It accepts the year, month, day, hour, minute, second, and millisecond components of the date and returns the number of milliseconds since January 1, 1970,
4 min read
JavaScript Adding hours to the Date object Given a date, the task is to add hours to it. To add hours to date in javascript, we're going to discuss a few techniques. First few methods to know. JavaScript getHours() Method: This method returns the hour (from 0 to 23) of the provided date and time. Syntax: Date.getHours() Parameters: This meth
3 min read
JavaScript Date setDate() Method The setDate() method in JavaScript is used to set the day of the month for a specified date object, according to local time. It allows you to update the day part of the date without changing other parts like month and year. Date setDate() SyntaxdateObj.setDate(date_Value);Date setDate() ParameterThi
2 min read
JavaScript Adding days in milliseconds to Date object Given a date, the task is to add days in milliseconds to it using JavaScript. To add days in milliseconds to date objects in JavaScript, some methods are used which are listed below: JavaScript getMilliseconds() Method: This method returns the milliseconds (from 0 to 999) of the provided date and ti
2 min read
JavaScript Date getDay() Method JavaScript Date getDay() Method is used to fetch the day of a week(0 to 6) from a given Date object.Syntax:DateObj.getDay()Parameters:This method does not accept any parameters.Return Values:It returns the day of the week for the given date. The day of the week will be returned in the form of an int
3 min read
How to calculate the yesterday's date in JavaScript ? In this article, we will see how to calculate yesterdayâs date in JavaScript. To calculate yesterday's date, you need to have some basic ideas of a few methods of JavaScript. JavaScript getDate() MethodJavaScript setDate() MethodJavaScript getDate() Method: It is an inbuilt JavaScript function that
3 min read
How to Subtract 7 Days from Current Date with MomentJS? The article is all about how can we get the 7 days subtracted date from the given date. for eg: if the given date is 28-08-2024 then it should print 21-08-2024, that is the exactly subtraction of the seven day from the given day. These are the following approaches:Table of ContentUsing the subtract(
2 min read
JavaScript Adding minutes to Date object Given a date and the task is to add minutes to it using JavaScript. To add minutes to the date object, some methods are used which are listed below: JavaScript getMinutes() Method: This method returns the Minutes (from 0 to 59) of the provided date and time. Syntax: Date.getMinutes() Parameters: Thi
3 min read
JavaScript Date getDate() Method The JavaScript getDate() method returns the day of the month (from 1 to 31) for a specified date according to local time. It's used with the Date object to extract and work with the day component of a date.Syntax:DateObj.getDate()Parameters:This method does not take any parameters.Return Value:Retur
4 min read