
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How do I subtract one week from this date in JavaScript?
You need to subtract one week i.e. 7 days from the current date. Following is the syntax −
var anyVariableName=new Date(yourCurrentDate.setDate(yourCurrentDate.getDate() - 7)
At first, get the current date −
var currentDate = new Date(); console.log("The current Date="+currentDate);
Now, set the new date with setDate() method and subtract 7 days −
Example
var currentDate = new Date(); console.log("The current Date="+currentDate); var before7Daysdate=new Date(currentDate.setDate(currentDate.getDate() - 7)); console.log("The One week ago date="+before7Daysdate);
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo60.js.
Output
This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo60.js The current Date=Tue Jul 14 2020 19:12:43 GMT+0530 (India Standard Time) The One week ago date=Tue Jul 07 2020 19:12:43 GMT+0530 (India Standard Time)
- Related Articles
- How do I subtract minutes from a date in JavaScript?
- Java Program to subtract week from current date
- How to subtract date from today's date in JavaScript?
- How to subtract days from a date in JavaScript?
- How can I subtract a day from a Python date?
- How do I get the current date in JavaScript?
- Finding day of week from date (day, month, year) in JavaScript
- How to subtract Python timedelta from date in Python?
- How do I get the current date and time in JavaScript?
- Subtract days from current date using Calendar.DATE in Java
- MySQL query to subtract date records with week day and display the weekday with records
- How to subtract one data frame from another in R?
- Java Program to subtract year from current date
- How to subtract number of days from a date to get the previous date in R?
- How do I shift from jQuery to core JavaScript?

Advertisements