Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Subtract Days from Date Object

JavaScript Subtract Days from Date Object

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)

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments