Learn how to work with JavaScript Date Objects in depth. Understand creation, manipulation, formatting, timezones, and more. Master the art of handling dates in JavaScript.
JavaScript provides a built-in Date object that allows you to work with dates and times. The Date object represents a single moment in time and can be used to perform various operations involving dates, times, and timezones.
Here’s an overview of how to work with JavaScript Date objects:
Creating Date Objects:
You can create a new Date object using one of the following methods:
javascript
// Current date and time const currentDate = new Date(); // Creating a specific date and time const specificDate = new Date('2023-08-07T10:00:00');
Getting Date Components:
You can access various components of a Date object using its methods:
javascript
const date = new Date(); const year = date.getFullYear(); const month = date.getMonth(); // Note: Month is 0-based (0 = January) const day = date.getDate(); const hours = date.getHours(); const minutes = date.getMinutes(); const seconds = date.getSeconds(); const milliseconds = date.getMilliseconds();
You can also set different components of a Date object:
javascript
date.setFullYear(2024); date.setMonth(3); // April (0-based) date.setDate(15); date.setHours(14); date.setMinutes(30); date.setSeconds(0); date.setMilliseconds(0);
JavaScript Date objects can be a bit tricky when dealing with timezones.
The browser’s timezone is used by default. You can convert a Date object to a specific timezone using libraries like moment-timezone or the Intl.DateTimeFormat API.
To format a Date object for display, you can use the toLocaleString() method or libraries like moment.js:
javascript
const formattedDate = date.toLocaleString('en-US', { timeZone: 'UTC' });
You can perform various date calculations by adding or subtracting milliseconds from a Date object:
javascript
const futureDate = new Date(); futureDate.setFullYear(futureDate.getFullYear() + 1);
You can compare two Date objects using standard comparison operators (<, >, <=, >=, ===). These comparisons are done based on the timestamp values.
Remember that JavaScript’s Date object has some quirks and limitations, especially when dealing with timezones and daylight saving time changes. Libraries like moment.js or modern JavaScript features can help mitigate these issues.
In modern JavaScript, there are also newer APIs like Intl.DateTimeFormat and Temporal that provide better date and time handling capabilities, but they might not be supported in all environments yet.
Complete Examples
Creating Date Objects: complete code in html
Here’s a complete HTML example that demonstrates how to create and display date objects using JavaScript:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Date Object Example</title> </head> <body> <h1>Date Object Example</h1> <p id="currentDate"></p> <p id="specificDate"></p> <script> // Current date and time const currentDate = new Date(); document.getElementById("currentDate").textContent = "Current Date: " + currentDate; // Creating a specific date and time const specificDate = new Date('2023-08-07T10:00:00'); document.getElementById("specificDate").textContent = "Specific Date: " + specificDate; </script> </body> </html>
Explanation:
1-In this example, we create two Date objects: one representing the current date and time, and the other representing a specific date and time (“2023-08-07T10:00:00”).
2-We then update the content of two HTML paragraphs (<p>) with the text representation of these date objects.
3-When you open this HTML file in a web browser, you should see the current date and time as well as the specific date and time displayed on the page.
Here’s a complete HTML example that demonstrates how to get date components from a Date object using JavaScript:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Date Components Example</title> </head> <body> <h1>Date Components Example</h1> <p id="dateComponents"></p> <script> const date = new Date(); const year = date.getFullYear(); const month = date.getMonth(); // Note: Month is 0-based (0 = January) const day = date.getDate(); const hours = date.getHours(); const minutes = date.getMinutes(); const seconds = date.getSeconds(); const milliseconds = date.getMilliseconds(); document.write("The date:",date,"<br>"); document.write("The year:",year,"<br>"); document.write("The month:",month,"<br>"); document.write("The day:",day,"<br>"); document.write("The hours:",hours,"<br>"); document.write("The minutes:",minutes,"<br>"); document.write("The seconds:",seconds,"<br>"); document.write("The milliseconds:",seconds,"<br>"); </script> </body> </html>
Explanation:
1-In this example, we create a Date object representing the current date and time.
2-We then use various get methods (like getFullYear(), getMonth(), etc.) to extract different date components.
3-The extracted components are then displayed as text in an HTML document.write() method
4-When you open this HTML file in a web browser, you should see the current date components displayed on the page.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Date Components Example</title> </head> <body> <h1>Date Components Example</h1> <p id="dateComponents"></p> <script> const date = new Date(); const year = date.getFullYear(); const month = date.getMonth(); // Note: Month is 0-based (0 = January) const day = date.getDate(); const hours = date.getHours(); const minutes = date.getMinutes(); const seconds = date.getSeconds(); const milliseconds = date.getMilliseconds(); alert(date); alert(year); alert(month); alert(day); alert(hours); alert(minutes); alert(yeseconds); alert(milliseconds); </script> </body> </html>
Here’s a complete HTML example that demonstrates how to set date components in a Date object using JavaScript:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Setting Date Components Example</title> </head> <body> <h1>Setting Date Components Example</h1> <p id="originalDate"></p> <p id="modifiedDate"></p> <script> const originalDate = new Date(); const modifiedDate = new Date(originalDate); modifiedDate.setFullYear(2024); modifiedDate.setMonth(3); // April (0-based) modifiedDate.setDate(15); modifiedDate.setHours(14); modifiedDate.setMinutes(30); modifiedDate.setSeconds(0); modifiedDate.setMilliseconds(0); document.getElementById("originalDate").textContent = "Original Date: " + originalDate; document.getElementById("modifiedDate").textContent = "Modified Date: " + modifiedDate; </script> </body> </html>
Explanation:
1-In this example, we first create a Date object representing the current date and time (originalDate).
2-Then, we create another Date object (modifiedDate) by copying the original date and then modifying its components using set methods.
3-Finally, we display both the original and modified dates on the page.
4-When you open this HTML file in a web browser, you should see the original and modified date objects displayed on the page.
Here’s an example of how you can work with timezones using JavaScript in an HTML file:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Timezone Example</title> </head> <body> <h1>Timezone Example</h1> <p id="utcTime"></p> <p id="localTime"></p> <script> const utcDate = new Date(); const localDate = new Date(); const utcTimeString = utcDate.toISOString(); // Convert to UTC string const localTimeString = localDate.toLocaleString(); // Convert to local time string document.getElementById("utcTime").textContent = "UTC Time: " + utcTimeString; document.getElementById("localTime").textContent = "Local Time: " + localTimeString; </script> </body> </html>
Explanation:
1-In this example, we create two Date objects: one representing the current date and time in UTC (utcDate), and the other representing the current date and time in the local timezone (localDate).
2-We then convert these date objects to strings using different methods to showcase the difference between UTC and local time.
3-When you open this HTML file in a web browser, you should see the UTC time and the local time displayed on the page. Keep in mind that the local time displayed will depend on your computer’s timezone setting.
There are several ways to create date objects in JavaScript. Here are some common methods:
You can create a new date object using the Date constructor. You can pass arguments to specify the year, month (0-11), day, hour, minute, second, and millisecond.
javascript
const currentDate = new Date(); // Current date and time const specificDate = new Date(2023, 7, 7, 10, 0, 0); // August 7, 2023, 10:00 AM
You can create a date object by parsing a date string using the Date.parse() function or the Date constructor. The date string should follow the ISO 8601 format.
javascript
const dateString = "2023-08-07T10:00:00"; const parsedDate = new Date(dateString);
You can create a date object using a timestamp, which represents the number of milliseconds since January 1, 1970 (UTC).
javascript
const timestamp = 1628348400000; // August 8, 2021, 10:00 AM UTC const timestampDate = new Date(timestamp);
JavaScript also provides methods to create date objects for specific scenarios, like getting the start or end of the month.
javascript
const startOfMonth = new Date(); startOfMonth.setDate(1); const endOfMonth = new Date(startOfMonth); endOfMonth.setMonth(endOfMonth.getMonth() + 1); endOfMonth.setDate(0);
There are various third-party libraries like moment.js and modern APIs like Temporal that offer more powerful and flexible ways to work with dates and times.
Remember that JavaScript’s built-in Date object has limitations, especially in handling timezones and date arithmetic. Consider using libraries or newer APIs for more complex date-related tasks.
JavaScript new Date():complete code in html
Here’s a complete HTML example that demonstrates how to create a date object using the new Date() constructor in JavaScript:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Date Object Example</title> </head> <body> <h1>Date Object Example</h1> <p id="currentDate"></p> <p id="specificDate"></p> <script> // Create a new Date object for the current date and time const currentDate = new Date(); document.getElementById("currentDate").textContent = "Current Date: " + currentDate; // Create a new Date object for a specific date and time const specificDate = new Date(2023, 7, 7, 10, 0, 0); // August 7, 2023, 10:00 AM document.getElementById("specificDate").textContent = "Specific Date: " + specificDate; </script> </body> </html>
Explanation:
1-In this example, we create two Date objects: one representing the current date and time using the new Date() constructor with no arguments, and the other representing a specific date and time using the constructor with year, month, day, hour, minute, and second arguments.
2-When you open this HTML file in a web browser, you should see the current date and time as well as the specific date and time displayed on the page.
new Date(date string):complete code in html
Here’s a complete HTML example that demonstrates how to create a date object using the new Date(dateString) constructor in JavaScript:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Date Object Example</title> </head> <body> <h1>Date Object Example</h1> <p id="parsedDate"></p> <script> // Create a new Date object by parsing a date string const dateString = "2023-08-07T10:00:00"; const parsedDate = new Date(dateString); document.getElementById("parsedDate").textContent = "Parsed Date: " + parsedDate; </script> </body> </html>
Explanation:
1-In this example, we create a Date object by parsing a date string using the new Date(dateString) constructor.
2-The date string follows the ISO 8601 format (“YYYY-MM-DDTHH:mm:ss”).
3-When you open this HTML file in a web browser, you should see the parsed date displayed on the page. The output will show the parsed date and time based on the provided date string.
new Date(year, month, …):complete code in html
Here’s a complete HTML example that demonstrates how to create a date object using the new Date(year, month, …) constructor in JavaScript:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Date Object Example</title> </head> <body> <h1>Date Object Example</h1> <p id="specificDate"></p> <script> // Create a new Date object for a specific date and time const specificDate = new Date(2023, 7, 7, 10, 0, 0); // August 7, 2023, 10:00 AM document.getElementById("specificDate").textContent = "Specific Date: " + specificDate; </script> </body> </html>
Explanation:
1-In this example, we create a Date object representing a specific date and time using the new Date(year, month, …) constructor.
2-You can pass the year, month (0-11), day, hour, minute, and second as arguments to this constructor.
3-When you open this HTML file in a web browser, you should see the specific date and time displayed on the page. The output will show the date and time based on the provided arguments to the constructor.
Here’s a complete HTML example that demonstrates how to create a date object using various combinations of numbers (6, 4, 3, or 2) in the new Date() constructor in JavaScript:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Date Object Example</title> </head> <body> <h1>Date Object Example</h1> <p id="date6"></p> <p id="date4"></p> <p id="date3"></p> <p id="date2"></p> <script> // Create a new Date object using 6 numbers (year, month, day, hour, minute, second) const date6 = new Date(2023, 7, 7, 10, 0, 0); document.getElementById("date6").textContent = "Date with 6 numbers: " + date6; // Create a new Date object using 4 numbers (year, month, day) const date4 = new Date(2023, 7, 7); document.getElementById("date4").textContent = "Date with 4 numbers: " + date4; // Create a new Date object using 3 numbers (year, month) const date3 = new Date(2023, 7); document.getElementById("date3").textContent = "Date with 3 numbers: " + date3; // Create a new Date object using 2 numbers (year) const date2 = new Date(2023); document.getElementById("date2").textContent = "Date with 2 numbers: " + date2; </script> </body> </html>
Explanation:
1-In this example, we create Date objects using different combinations of numbers as arguments to the new Date() constructor.
2-Each combination specifies different levels of detail: year, month, day, hour, minute, and second.
3-When you open this HTML file in a web browser, you should see the date objects displayed for each combination of numbers. 4-The output will show the date and time based on the provided arguments to the constructor.
Here’s a complete HTML example that demonstrates how to create a date object representing a date from the previous century using the new Date() constructor in JavaScript:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Previous Century Date Example</title> </head> <body> <h1>Previous Century Date Example</h1> <p id="previousCenturyDate"></p> <script> // Create a new Date object for a date in the previous century const previousCenturyDate = new Date(1900, 0, 1); // January 1, 1900 document.getElementById("previousCenturyDate").textContent = "Previous Century Date: " + previousCenturyDate; </script> </body> </html>
Explanation:
1-In this example, we create a Date object representing a date from the previous century (January 1, 1900) using the new Date() constructor.
2-We provide the year, month (0-11), and day as arguments to create the date object.
3-When you open this HTML file in a web browser, you should see the date from the previous century displayed on the page. 4-The output will show the date and time based on the provided arguments to the constructor.
Here’s a complete HTML example that demonstrates how to create a date object using the new Date(milliseconds) constructor in JavaScript:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Date Object Example</title> </head> <body> <h1>Date Object Example</h1> <p id="millisecondsDate"></p> <script> // Create a new Date object using milliseconds since January 1, 1970 (UTC) const milliseconds = 1628348400000; // August 8, 2021, 10:00 AM UTC const millisecondsDate = new Date(milliseconds); document.getElementById("millisecondsDate").textContent = "Milliseconds Date: " + millisecondsDate; </script> </body> </html>
Explanation:
1-In this example, we create a Date object using the new Date(milliseconds) constructor by providing the number of milliseconds since January 1, 1970 (UTC).
2-This represents a specific point in time.
3-When you open this HTML file in a web browser, you should see the date and time corresponding to the provided milliseconds displayed on the page.
4-The output will show the date and time based on the provided milliseconds value.
Here’s a complete HTML example that demonstrates how to format and display date objects using various methods in JavaScript:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Displaying Dates Example</title> </head> <body> <h1>Displaying Dates Example</h1> <p id="toLocaleString"></p> <p id="toDateString"></p> <p id="toTimeString"></p> <p id="toUTCString"></p> <script> const date = new Date(); // Display using toLocaleString() const toLocaleString = date.toLocaleString('en-US', { timeZone: 'UTC' }); // Display using toDateString() const toDateString = date.toDateString(); // Display using toTimeString() const toTimeString = date.toTimeString(); // Display using toUTCString() const toUTCString = date.toUTCString(); document.getElementById("toLocaleString").textContent = "toLocaleString(): " + toLocaleString; document.getElementById("toDateString").textContent = "toDateString(): " + toDateString; document.getElementById("toTimeString").textContent = "toTimeString(): " + toTimeString; document.getElementById("toUTCString").textContent = "toUTCString(): " + toUTCString; </script> </body> </html>
Explanation:
1-In this example, we create a Date object representing the current date and time.
2-We then use different methods like toLocaleString(), toDateString(), toTimeString(), and toUTCString() to format and display the date object in various ways.
3-When you open this HTML file in a web browser, you should see the different formatted date and time representations displayed on the page. Each paragraph (<p>) will show the output of one of the formatting methods.
Here’s a complete HTML example that combines the concepts from the previous responses:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Date Handling Application</title> </head> <body> <h1>Date Handling Application</h1> <p>Current Date and Time: <span id="currentDate"></span></p> <p>Specific Date: <span id="specificDate"></span></p> <p>Date Components: <span id="dateComponents"></span></p> <p>Formatted Date: <span id="formattedDate"></span></p> <script> // Create a new Date object for the current date and time const currentDate = new Date(); document.getElementById("currentDate").textContent = currentDate; // Create a new Date object for a specific date and time const specificDate = new Date(2023, 7, 7, 10, 0, 0); // August 7, 2023, 10:00 AM document.getElementById("specificDate").textContent = specificDate; // Get date components const year = currentDate.getFullYear(); const month = currentDate.getMonth() + 1; // Month is 0-based, so add 1 const day = currentDate.getDate(); const hours = currentDate.getHours(); const minutes = currentDate.getMinutes(); const seconds = currentDate.getSeconds(); const dateComponents = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`; document.getElementById("dateComponents").textContent = dateComponents; // Format date using toLocaleString() const formattedDate = currentDate.toLocaleString('en-US', { timeZone: 'UTC' }); document.getElementById("formattedDate").textContent = formattedDate; </script> </body> </html>
Explanation:
1-In this example, we’ve combined the concepts of creating date objects, extracting date components, and formatting dates.
2-This simple application displays the current date and time, a specific date, date components, and a formatted date using different methods.
3-Each section of information is displayed using the textContent property of the respective HTML elements.
3-When you open this HTML file in a web browser, you should see the different date-related information displayed on the page. 4-This example serves as a basic demonstration of how to work with date objects and related operations in JavaScript.
Creating a complete multi-choice quiz with answers requires a more comprehensive code structure, potentially involving JavaScript, HTML, and CSS. Here’s a simplified example of a multi-choice quiz with answers using these technologies:
index.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Multi-Choice Quiz</title> <link rel="stylesheet" href="styles.css"> </head> <body> <h1>Multi-Choice Quiz</h1> <div id="quiz-container"> <div id="question"></div> <div id="options"></div> <button id="submit-button">Submit</button> </div> <div id="result"></div> <script src="script.js"></script> </body> </html>
CSS file:
styles.css:
css code
body { font-family: Arial, sans-serif; margin: 0; padding: 20px; } h1 { text-align: center; } #quiz-container { border: 1px solid #ccc; padding: 20px; margin-top: 20px; } #options { margin-top: 10px; } button { margin-top: 10px; } #result { margin-top: 20px; text-align: center; } script.js: javascript code
script.js
const questionElement = document.getElementById("question"); const optionsElement = document.getElementById("options"); const submitButton = document.getElementById("submit-button"); const resultElement = document.getElementById("result"); const questions = [ { question: "What is the capital of France?", options: ["London", "Berlin", "Paris", "Madrid"], answer: "Paris", }, { question: "Which planet is known as the Red Planet?", options: ["Earth", "Mars", "Jupiter", "Venus"], answer: "Mars", }, // Add more questions here ]; let currentQuestionIndex = 0; function showQuestion(index) { const question = questions[index]; questionElement.textContent = question.question; optionsElement.innerHTML = ""; question.options.forEach((option, optionIndex) => { const optionButton = document.createElement("button"); optionButton.textContent = option; optionButton.addEventListener("click", () => selectOption(option, optionIndex)); optionsElement.appendChild(optionButton); }); } function selectOption(selectedOption, selectedOptionIndex) { if (selectedOption === questions[currentQuestionIndex].answer) { resultElement.textContent = "Correct!"; } else { resultElement.textContent = "Incorrect!"; } currentQuestionIndex++; if (currentQuestionIndex < questions.length) { showQuestion(currentQuestionIndex); } else { questionElement.textContent = ""; optionsElement.innerHTML = ""; submitButton.disabled = true; resultElement.textContent = "Quiz completed!"; } } submitButton.addEventListener("click", () => showQuestion(currentQuestionIndex)); // Start the quiz showQuestion(currentQuestionIndex);
This example includes three files: index.html, styles.css, and script.js.
It creates a simple multi-choice quiz where the questions are displayed one by one, and the can select an answer. After answering all questions, the receives a completion message.
1-What does the Date object in JavaScript represent?
Answer: c) A single moment in time
2-How can you create a Date object representing the current date and time?
Answer: d) new Date()
3-What is the purpose of the getMonth() method in the Date object?
Answer: b) Get the index of the current month (0-based)
4-How can you set the year of a Date object to 2024?
Answer: b) date.setFullYear(2024)
5-What method can be used to format a Date object for display in a specific timezone?
Answer: a) toLocaleString()
6-Which of the following methods is used to get the day of the week from a Date object?
Answer: c) getDay()
7-What does the term “epoch time” refer to in the context of Date objects?
Answer: c) The number of milliseconds since January 1, 1970 (UTC)
8-Which method can be used to calculate the difference in milliseconds between two Date objects?
Answer: d) getTime()
9-Why might using libraries like moment.js or the Temporal API be preferred over the built-in Date object?
Answer: c) They handle timezones and date arithmetic more effectively
10-What is the purpose of the UTC timezone in relation to Date objects?
Answer: c) It’s used to display dates in Coordinated Universal Time (UTC)
11-Which of the following is NOT a valid way to set the month of a Date object?
Answer: b) date.month = 2
12-How can you get the current timestamp (number of milliseconds since epoch time) using the Date object?
Answer: d) Date.now()
13-Which of the following methods can be used to compare two Date objects for equality?
Answer: c) date === otherDate
14-What is the significance of the Unix epoch time starting from January 1, 1970?
Answer: c) It’s a commonly used reference point for measuring time in computing
15-How can you add 3 days to a Date object?
Answer: b) date.setDate(date.getDate() + 3)
16-What does the term “leap year” refer to?
Answer: b) A year with 366 days
17-Which method is used to get the number of days in a specific month of a Date object?
Answer: a) date.getDaysInMonth()
18-How can you retrieve the milliseconds component of a Date object?
Answer: b) date.getMilliseconds()
19-What is the purpose of the Date.parse() method?
Answer: a) To create a new Date object from a string
20-How can you determine if a specific year is a leap year using JavaScript?
Answer: b) Check if the year is divisible by 4
21-What value does the getDay() method return for Sunday?
Answer: a) 0
22-Which of the following methods can be used to set the day of the month for a Date object?
Answer: c) date.setDate()
23-How can you calculate the difference between two dates in days using the Date object?
Answer: a) By subtracting one date from the other and converting to days
24-What is the output of the following code snippet?
javascript
const date = new Date();
const formattedDate = date.toISOString();
console.log(formattedDate);
Answer: a) Current date and time in UTC
25-Which method can be used to get the total number of milliseconds between two Date objects?
Answer: c) Math.abs(date1 – date2)
26-Which of the following methods can be used to get the current year of a Date object?
Answer: c) date.getFullYear()
27-How can you convert a Date object to a string representing the date in ISO 8601 format?
Answer: b) date.toISOString()
28-Which method can be used to retrieve the time zone offset of the current system’s time zone?
Answer: d) date.getTimezoneOffset()
29-How can you set the time of day to 3:30 PM for a Date object?
Answer: b) date.setHours(15, 30)
30-What is the result of the expression new Date(2022, 0, 1) > new Date(2023, 0, 1)?
Answer: b) false
References of this lesson
MDN Web Docs – Date: The Mozilla Developer Network provides detailed documentation on the Date object in JavaScript, including its properties and methods.
MDN Web Docs – Date
W3Schools – JavaScript Date Object: W3Schools offers interactive tutorials and examples for learning about the Date object in JavaScript.
W3Schools – JavaScript Date Object
ECMAScript Language Specification: The official ECMAScript language specification includes details about the Date object and its behavior.
ECMAScript Language Specification – Date Objects
Moment.js (Legacy Library): Although Moment.js is considered a legacy library, it was widely used for handling dates and times. It’s still valuable for understanding date manipulation concepts.
Moment.js
Temporal (Modern API): Temporal is a modern date and time API proposal for JavaScript that aims to overcome some of the limitations of the built-in Date object.