![]() |
VOOZH | about |
The JavaScript Date object represents a specific moment in time, measured in milliseconds since the Unix Epoch (January 1, 1970). It's crucial for working with date and time in web applications, providing methods for tasks like date manipulation, formatting, and calculations.
In JavaScript, the Date object tracks time as the number of milliseconds since the Unix Epoch. You can create a Date object using the new Date() constructor, which allows you to specify either the current date and time or a particular date.
The Date object can be initialized in various ways using the new Date() constructor. It supports multiple formats for defining dates:
new Date();
new Date(value);
new Date(dateString);
new Date(year, month, day, hours, minutes, seconds, milliseconds);
1. Current Date and Time
const currentDate = new Date();2. Milliseconds since Epoch
const dateFromMilliseconds = new Date(1609459200000); // Represents January 1, 20213. Date String
const dateFromString = new Date("March 25, 2024");4. Specific Date and Time
const specificDate = new Date(2023, 10, 13, 5, 30, 22); // Represents November 13, 2023, 5:30:22 AMThe Date constructor allows for different parameter types to define a specific date and time. Here’s a breakdown of the possible parameters:
| Field | Description |
|---|---|
| value | The number of milliseconds since January 1, 1970, 00:00:00 UTC. |
| dateString | Represents a date format. |
| year | An integer representing the year, ranging from 1900 to 1999. |
| month | An integer representing the month, ranging from 0 for January to 11 for December. |
| day | An optional integer representing the day of the month. |
| hours | An optional integer representing the hour of the day. |
| minutes | An optional integer representing the minute of the time. |
| seconds | An optional integer representing the second of the time. |
| milliseconds | An optional integer representing the millisecond of the time. |
You can retrieve various parts of a date using the following methods:
You can format dates manually or use built-in options such as Intl.DateTimeFormat to display the date in a more readable format.
7/8/2024
You can modify dates using various methods provided by the Date object, such as:
The code initializes a Date object representing the current date. It then increments the date by 7 days using setDate(). Finally, it logs the modified date to the console.
2024-07-15T10:25:17.602Z
The code initializes a `Date` object with the provided parameters: year (1996), month (10 for November), day (13), hours (5), minutes (30), seconds (22), and milliseconds (0 by default). It then logs this date to the console.
1996-11-13T05:30:22.000Z
We have a complete list of Javascript Date object methods, to check those please go through this JavaScript Date Object Complete Reference article.
The browsers supported by JavaScript Date are listed below:
Note: Ensure your browser is updated to the latest version for full compatibility and optimal performance when working with JavaScript Date functionalities..