How to Get Last Week's Date using JavaScript ?
Last Updated :
12 Sep, 2023
In this article, we are going to learn about how to get last week's Date by using JavaScriptthe. JavaScript's dynamic nature improves websites by adding dynamic elements. Fetching the previous week's date is vital for event schedules, calendars, and timely data display.
There are several methods that can be used to get the last week's Date using JavaScript, which is listed below:
- Leveraging the Date Objects
- Using seÂtUTCDate Method
We will explore all the above methods along with their basic implementation with the help of examples.
The JavaScript Date object offers a variety of meÂthods that simplify date operations. To retrieÂve the date from the previous week, one can subtract 7 days' worth of milliseconds from the current date and elegantly display it on the screÂen.
Syntax:
const lastWeekDate = new Date(currentDate.getTime() - 7 * 24 * 60 * 60 * 1000);
Example: The provideÂd HTML code is responsible for creÂating a web page that prominently showcaseÂs the text "GeeÂksforgeeks" as its main heading. To eÂnsure an organized layout with centeÂred content, CSS styling techniqueÂs are employed, compleÂmented by a background color. A visually appealing box is also includeÂd to highlight the "Last Week's DateÂ," which is dynamically calculated and displayed using JavaScript within this designateÂd space.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Last Week's Date</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
#dateContainer {
background-color: #ffffff;
border-radius: 8px;
padding: 16px;
box-shadow: 0px 2px 6px rgba(0, 0, 0, 0.1);
width: 300px;
height: 150px;
text-align: center;
}
.heading {
color: green;
font-size: 26px;
border-bottom: 3px solid green;
border-radius: 15px;
padding: 10px;
text-align: center;
}
.subheading {
font-size: 20px;
}
</style>
</head>
<body>
<div id="dateContainer">
<h1 class="heading">
Geeksforgeeks
</h1>
<h2 class="subheading">
Last Week's Date:
</h2>
<p id="lastWeekDate"></p>
</div>
<script>
// Get last week's date using Date object
const currentDate = new Date();
const lastWeekDate = new Date(currentDate.getTime()
- 7 * 24 * 60 * 60 * 1000);
// Display last week's date on the screen
const lastWeekDateElement =
document.getElementById("lastWeekDate");
lastWeekDateElement.textContent =
lastWeekDate.toDateString();
</script>
</body>
</html>
Output:

In this approach, we will use the `seÂtUTCDate` method, this approach calculates the date of the previous weÂek by subtracting 7 days directly from the curreÂnt date. Subsequently, it formats the result as "YYYY-MM-DD". The JavaScript `Date` objeÂct is effectively eÂmployed to manipulate dates and provideÂs an alternative means of obtaining last weÂek's date.
Syntax:
currentDate.setUTCDate(currentDate.getUTCDate() - 7);
Example: This HTML code is useÂd to create a webpage with the title "Last WeeÂk's Date." By utilizing CSS, the layout is structured neÂatly with centered conteÂnt and a green-theÂmed design. The weÂb page showcases the title "GeeksforgeeÂks," followed by "Last Week's DateÂ:" along with the date from one weÂek ago. All of this is presenteÂd within a container that features subtle shadow effects.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<title>Last Week's Date</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
#dateContainer {
background-color: #ffffff;
border-radius: 8px;
padding: 16px;
box-shadow: 0px 2px 6px rgba(0, 0, 0, 0.1);
width: 300px;
height: 150px;
text-align: center;
}
.heading {
color: green;
font-size: 26px;
border-bottom: 3px solid green;
border-radius: 15px;
padding: 10px;
text-align: center;
}
.subheading {
font-size: 20px;
}
</style>
</head>
<body>
<div id="dateContainer">
<h1 class="heading">
Geeksforgeeks
</h1>
<h2 class="subheading">
Last Week's Date:
</h2>
<p id="lastWeekDate"></p>
</div>
<script>
// Get last week's date using JavaScript
const currentDate = new Date();
currentDate.setUTCDate(currentDate.getUTCDate() - 7);
// Format the date as "YYYY-MM-DD"
const formattedDate =
`${currentDate.getUTCFullYear()} -
${(currentDate.getUTCMonth() + 1)
.toString().padStart(2, '0')
} - ${currentDate.getUTCDate()
.toString().padStart(2, '0')}`;
// Display last week's date on the screen
const lastWeekDateElement =
document.getElementById("lastWeekDate");
lastWeekDateElement.textContent = formattedDate;
</script>
</body>
</html>
Output:
Get last week's Date using JavaScript Example 2
Similar Reads
How to get the current year using JavaScript ?
In this article, we will know how to get the current year using the build-in method in Javascript. The purpose is to get the current year by using JavaScript getFullYear() method that will return the full year in 4-digit format for some specified date. This method is used to fetch the year from a gi
1 min read
How to get the month name from Date using JavaScript ?
The purpose of this article is to get the month name from a particular date using JavaScript getMonth() method. This method is used to fetch the month(0 to 11) from the given Date object. It returns an integer value ranging from (0 to 11) Zero (0) means January, 1 means February, and so on, till 11
1 min read
How to get the day and month of a year using JavaScript ?
Given a date and the task is to get the day and month of a year using JavaScript. Approach: First get the current date by using new Date().Use getDay() method to get the current day in number format, Map it to the day name.Use getMonth() to get the current month in number format, Map it to the month
2 min read
How to remove time from date using JavaScript ?
Given a Date Object and the task is to remove the time portion from the object using JavaScript. JavaScript split() Method: This method is used to split a string into an array of substrings, and returns the new array. Syntax: string.split( separator, limit ) Parameters: separator: This parameter is
2 min read
How to get the first and last date of current month using JavaScript ?
Given a month and the task is to determine the first and last day of that month in proper format using JavaScript. 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.
3 min read
How to Get the Current Date in JavaScript ?
The Date() object is used to access the current date in JavaScript. To get the current date in string format, we can use toDateString() method. The date.toDateString() method converts the given date object into the date portion into a string.SyntaxdateObj.toDateString()JavaScript// Create a Date Obj
2 min read
JavaScript Get Date Methods
JavaScript Date Object allows us to get and set the date values. In this article, we will know how to get the various date methods from the date object in Javascript. There are various methods that retrieve the date in JavaScript. The date values can get like years, months, days, hours, minutes, sec
3 min read
How to convert UTC date time into local date time using JavaScript ?
Given an UTC date and the task is to convert UTC date time into local date-time using JavaScript toLocaleString() function. Syntax: var theDate = new Date(Date.parse('DATE_IN_UTC')) theDate.toLocaleString() Example 1: This example converts UTC date time into local date time using JavaScript. html
1 min read
How to get tomorrow's date in a string format in JavaScript ?
In this article, we will see how to print tomorrow's date in string representation using JavaScript. To achieve this, we use the Date object and create an instance of it. After that by using the setDate() method, we increase one date to the present date. Now by using the getDate() method you will ge
2 min read
Convert string into date using JavaScript
In this article, we will convert a string into a date using JavaScript. A string must follow the pattern so that it can be converted into a date. A string can be converted into a date in JavaScript through the following ways: Table of Content Using JavaScript Date() constructorUsing JavaScript toDat
3 min read