Syntax
To create a Date object in JavaScript, use the new Date() constructor. This function can be used in several ways depending on the parameters passed. Below are the common syntaxes:
new Date() // Creates a new date object with the current date and time.
new Date(milliseconds) // Creates a date object based on the number of milliseconds since January 1, 1970.
new Date(dateString) // Creates a date object from a string representation of a date.
new Date(year, month, ...) // Creates a date object with specified year, month, and other parameters.
Example: new Date()
The simplest way to create a Date object is by calling it without any arguments.
const currentDate = new Date();
console.log(currentDate);

You can also try this code with Online Javascript Compiler
Run Code
Output:
Wed Jan 07 2025 10:30:45 GMT+0000 (Coordinated Universal Time)
Explanation:
This creates a date object representing the current date and time based on the system clock.
Example: new Date(milliseconds)
The new Date(milliseconds) method creates a date object based on the number of milliseconds since January 1, 1970.
const dateFromMilliseconds = new Date(0);
console.log(dateFromMilliseconds);

You can also try this code with Online Javascript Compiler
Run Code
Output:
Thu Jan 01 1970 00:00:00 GMT+0000 (Coordinated Universal Time)
Explanation:
Here, 0 represents the epoch time (January 1, 1970). You can pass any positive or negative number to represent a date relative to the epoch.
Example: new Date(dateString)
The new Date(dateString) method parses a string and returns the corresponding date.
const dateFromString = new Date("2025-01-07");
console.log(dateFromString);

You can also try this code with Online Javascript Compiler
Run Code
Output:
Tue Jan 07 2025 00:00:00 GMT+0000 (Coordinated Universal Time)

You can also try this code with Online Javascript Compiler
Run Code
Explanation:
This creates a Date object based on the provided ISO 8601 date string.
Example: new Date(year, month, date, hour, minute, second, millisecond)
This method lets you define a specific date and time by passing multiple parameters.
const specificDate = new Date(2025, 0, 7, 10, 30, 45);
console.log(specificDate);

You can also try this code with Online Javascript Compiler
Run Code
Output:
Tue Jan 07 2025 10:30:45 GMT+0000 (Coordinated Universal Time)
Explanation:
- 2025 is the year.
- 0 represents January (months are zero-based).
- 7 is the day.
- The remaining arguments specify hours, minutes, seconds, and milliseconds.
Properties of Date Object
The Date object comes with several properties that allow you to retrieve different parts of the date and time. Here are the most common ones:
Examples
getFullYear()
Returns the year of the specified date.
const date = new Date();
console.log(date.getFullYear());

You can also try this code with Online Javascript Compiler
Run Code
Output:
2025
getMonth()
Returns the month (0 for January, 11 for December).
console.log(date.getMonth());

You can also try this code with Online Javascript Compiler
Run Code
Output:
0 (January)
getDate()
Returns the day of the month.
console.log(date.getDate());

You can also try this code with Online Javascript Compiler
Run Code
Output:
7
getDay()
Returns the day of the week (0 for Sunday, 6 for Saturday).
console.log(date.getDay());

You can also try this code with Online Javascript Compiler
Run Code
Output:
2 (Tuesday)
getTime()
Returns the number of milliseconds since January 1, 1970.
console.log(date.getTime());

You can also try this code with Online Javascript Compiler
Run Code
Example:
1736275845000
Creating a Date Object
You can create a Date object in multiple ways. Let’s look at some examples:
1. Current Date & Time: If you don’t pass any arguments, the Date object will represent the current date & time.
let currentDate = new Date();
console.log(currentDate);
When you run this code, it will print the current date & time in your browser’s default format.
2. Specific Date & Time: You can create a Date object for a specific date & time by passing arguments. The syntax is:
new Date(year, month, day, hours, minutes, seconds, milliseconds);
For example:
let specificDate = new Date(2023, 9, 15, 14, 30, 0); // October 15, 2023, 2:30:00 PM
console.log(specificDate);
Note: The `month` parameter is zero-based, meaning January is 0, February is 1, & so on. In this example, `9` represents October.
3. Using a Date String: You can also create a Date object by passing a date string.
let dateString = new Date("2023-10-15T14:30:00");
console.log(dateString);
This will create a Date object for October 15, 2023, at 2:30:00 PM.
4. Using Milliseconds: You can create a Date object by passing the number of milliseconds since the Unix epoch.
let dateMillis = new Date(1697308200000); // October 15, 2023, 2:30:00 PM in milliseconds
console.log(dateMillis);
This will give you the same result as the previous example.
Date Object Methods
The Date object provides several methods for manipulating dates and times. Here are some of the most commonly used ones:
setFullYear()
Sets the year for a specified date.
const date = new Date();
date.setFullYear(2023);
console.log(date);

You can also try this code with Online Javascript Compiler
Run Code
Output:
Sat Jan 07 2023 10:30:45 GMT+0000 (Coordinated Universal Time)
setMonth()
Sets the month for a specified date.
date.setMonth(5); // Sets the month to June
console.log(date);

You can also try this code with Online Javascript Compiler
Run CodetoDateString()
Returns the date portion of the Date object as a human-readable string.
console.log(date.toDateString());

You can also try this code with Online Javascript Compiler
Run Code
Output:
Sat Jun 07 2023
toISOString()
Converts the Date object to an ISO 8601 string.
console.log(date.toISOString());

You can also try this code with Online Javascript Compiler
Run Code
Output:
2023-06-07T10:30:45.000Z
toLocaleDateString()
Formats the date according to local conventions.
console.log(date.toLocaleDateString("en-US"));

You can also try this code with Online Javascript Compiler
Run Code
Output
6/7/2023
Methods of the Date Object
The Date object comes with many methods to get & set date & time values. Let’s look at some commonly used ones:
- getFullYear(): Returns the year (4 digits).
getMonth(): Returns the month (0-11).
- getDate(): Returns the day of the month (1-31).
- getHours(): Returns the hour (0-23).
- getMinutes(): Returns the minutes (0-59).
- getSeconds(): Returns the seconds (0-59).
- getMilliseconds(): Returns the milliseconds (0-999).
Example
let date = new Date();
console.log(date.getFullYear()); // Current year
console.log(date.getMonth()); // Current month (0-11)
console.log(date.getDate()); // Current day of the month
console.log(date.getHours()); // Current hour
console.log(date.getMinutes()); // Current minutes
console.log(date.getSeconds()); // Current seconds
You can also use the `set` methods to modify the date & time. For example:
let date = new Date();
date.setFullYear(2024);
date.setMonth(11); // December (11)
date.setDate(25);
console.log(date); // December 25, 2024
Using 6, 4, 3, or 2 Numbers
When creating a Date object, you can pass different numbers of arguments to represent dates & times. The number of arguments you pass determines how the Date object interprets them. Let’s discuss this in detail:
1. Using 6 Numbers
If you pass 6 numbers, they represent the year, month, day, hours, minutes, & seconds. Here’s the syntax:
new Date(year, month, day, hours, minutes, seconds);
Example:
let dateWith6Numbers = new Date(2023, 9, 15, 14, 30, 45);
console.log(dateWith6Numbers);

You can also try this code with Online Javascript Compiler
Run Code
Output:
Sun Oct 15 2023 14:30:45 GMT+0530 (India Standard Time)
In this example:
- `2023` is the year.
- `9` is the month (October, since months are zero-based).
- `15` is the day.
- `14` is the hour (2 PM).
- `30` is the minutes.
- `45` is the seconds.
2. Using 4 Numbers
If you pass 4 numbers, they represent the year, month, day, & hours. The minutes & seconds are set to 0 by default.
new Date(year, month, day, hours);
Example:
let dateWith4Numbers = new Date(2023, 9, 15, 14);
console.log(dateWith4Numbers);

You can also try this code with Online Javascript Compiler
Run Code
Output:
Sun Oct 15 2023 14:00:00 GMT+0530 (India Standard Time)
In this code:
- `2023` is the year.
- `9` is the month (October)
- `15` is the day.
- `14` is the hour (2 PM).
- Minutes & seconds are set to `0` by default.
3. Using 3 Numbers
If you pass 3 numbers, they represent the year, month, & day. The time is set to 00:00:00 by default.
new Date(year, month, day);
Example:
let dateWith3Numbers = new Date(2023, 9, 15);
console.log(dateWith3Numbers);

You can also try this code with Online Javascript Compiler
Run Code
Output:
Sun Oct 15 2023 00:00:00 GMT+0530 (India Standard Time)
In this example:
- `2023` is the year.
- `9` is the month (October)
- `15` is the day.
- Time is set to `00:00:00` by default.
4. Using 2 Numbers
If you pass 2 numbers, they represent the year & month. The day is set to 1, & the time is set to 00:00:00 by default.
new Date(year, month);
Example:
let dateWith2Numbers = new Date(2023, 9);
console.log(dateWith2Numbers);

You can also try this code with Online Javascript Compiler
Run Code
Output:
Sun Oct 01 2023 00:00:00 GMT+0530 (India Standard Time)
In this example:
- `2023` is the year.
- `9` is the month (October).
- Day is set to `1` by default.
- Time is set to `00:00:00` by default.
Key Points to Remember
- The `month` parameter is zero-based (0 = January, 1 = February, ..., 11 = December).
- If you omit any parameter, it defaults to the lowest possible value (e.g., day = 1, time = 00:00:00).
- You can use these variations depending on how much detail you need in your date & time.
Previous Century
The JavaScript Date object can also handle dates from the previous century (1900s). This is particularly useful when working with historical data or older systems. Let’s understand how to create & work with dates from the 1900s.
Creating Dates from the 1900s
To create a date from the 1900s, you can use the same `new Date()` constructor. However, there’s a special rule for the `year` parameter:
If the `year` is between `0` & `99`, JavaScript interprets it as `1900 + year`. For example:
- `0` means `1900`.
- `50` means `1950`.
- `99` means `1999`.
Let’s see how you can create dates from the 1900s:
let date1900 = new Date(0, 0, 1); // January 1, 1900
console.log(date1900);
let date1950 = new Date(50, 0, 1); // January 1, 1950
console.log(date1950);
let date1999 = new Date(99, 11, 31); // December 31, 1999
console.log(date1999);

You can also try this code with Online Javascript Compiler
Run Code
Output:
Mon Jan 01 1900 00:00:00 GMT+0530 (India Standard Time)
Sun Jan 01 1950 00:00:00 GMT+0530 (India Standard Time)
Fri Dec 31 1999 00:00:00 GMT+0530 (India Standard Time)
Working with Dates from the 1900s
You can use all the standard Date object methods to work with these dates. For example:
1. Get the Year: Use `getFullYear()` to get the full year (e.g., `1950` instead of `50`).
let date1950 = new Date(50, 0, 1);
console.log(date1950.getFullYear()); // 1950
2. Calculate the Difference Between Two Dates: You can subtract two Date objects to get the difference in milliseconds, then convert it to years, days, etc.
let date1950 = new Date(50, 0, 1);
let date1999 = new Date(99, 11, 31);
let differenceInMillis = date1999 - date1950;
let differenceInYears = differenceInMillis / (1000 * 60 * 60 * 24 * 365.25); // Accounting for leap years
console.log(differenceInYears); // Approximately 49.997 years
3. Format the Date: You can format the date as a string using methods like `toDateString()` or `toLocaleDateString()`.
let date1950 = new Date(50, 0, 1);
console.log(date1950.toDateString()); // "Sun Jan 01 1950"
console.log(date1950.toLocaleDateString()); // "1/1/1950" (format depends on your locale)
Key Points to Remember
- For years between `0` & `99`, JavaScript adds `1900` to the year.
- You can use all standard Date methods (`getFullYear()`, `getMonth()`, etc.) with dates from the 1900s.
- Be careful when calculating differences between dates, as leap years can affect the results.
Browser Compatibility
The JavaScript Date object is widely supported across all modern browsers, including Chrome, Firefox, Safari, Edge, & Opera. However, there are some nuances & differences in how browsers handle certain Date object features, especially when it comes to parsing date strings. Let’s take a look at this in detail.
General Compatibility
The basic functionality of the Date object, such as creating dates, getting & setting date components, & performing calculations, works consistently across all browsers. For example:
let currentDate = new Date();
console.log(currentDate.getFullYear()); // Works in all browsers
This code will output the current year in any modern browser.
Parsing Date Strings
One area where browser differences can arise is in parsing date strings. Different browsers may interpret date strings differently, especially if the format is not standardized. For example:
let dateString = new Date("2023-10-15");
console.log(dateString);

You can also try this code with Online Javascript Compiler
Run Code
In most modern browsers, this will output:
Sun Oct 15 2023 05:30:00 GMT+0530 (India Standard Time)
However, older browsers or certain environments might interpret this differently.
To avoid inconsistencies, it’s recommended to use the ISO 8601 format (`YYYY-MM-DDTHH:mm:ss.sssZ`) for date strings. This format is universally supported:
let isoDate = new Date("2023-10-15T14:30:00Z");
console.log(isoDate);

You can also try this code with Online Javascript Compiler
Run Code
Output:
Sun Oct 15 2023 20:00:00 GMT+0530 (India Standard Time)
Timezone Handling
Browsers may also handle timezones differently. For example, if you create a Date object without specifying a timezone, the browser will use the local timezone of the user’s system:
let localDate = new Date(2023, 9, 15, 14, 30);
console.log(localDate);

You can also try this code with Online Javascript Compiler
Run Code
Output (in India):
Sun Oct 15 2023 14:30:00 GMT+0530 (India Standard Time)
If you need to work with UTC (Coordinated Universal Time), you can use methods like `getUTC*` & `setUTC*`:
let utcDate = new Date(Date.UTC(2023, 9, 15, 14, 30));
console.log(utcDate.toUTCString());

You can also try this code with Online Javascript Compiler
Run Code
Output:
Sun, 15 Oct 2023 14:30:00 GMT
Checking Browser Compatibility
If you’re working on a project that needs to support older browsers, you can check for compatibility using tools like [Can I use](https://caniuse.com/). For example, the `Date.prototype.toLocaleDateString()` method has some variations in older browsers:
let date = new Date();
console.log(date.toLocaleDateString('en-US', { weekday: 'long' }));
- In modern browsers, this will output the full weekday name (e.g., `Sunday`).
- In older browsers, it might not support the `weekday` option.
Polyfills for Older Browsers
If you need to support older browsers, you can use polyfills or libraries like [Moment.js](https://momentjs.com/) or [date-fns](https://date-fns.org/) to ensure consistent behavior.
Example using Moment.js
let momentDate = moment("2023-10-15");
console.log(momentDate.format("MMMM Do YYYY")); // October 15th 2023
Frequently Asked Questions
What is the JavaScript Date object used for?
The JavaScript Date object is used to work with dates and times. It allows you to create, modify, and display date and time information in various formats.
Why is the month zero-based in the Date object?
JavaScript uses a zero-based index for months (0 for January to 11 for December) to align with array indexing and other programming conventions.
Can I format a date directly with the Date object?
The Date object does not have built-in formatting functions. Use methods like toLocaleDateString() or external libraries like Moment.js or date-fns for advanced formatting.
Conclusion
The JavaScript Date object is an essential part of working with dates and times in your applications. From creating and manipulating dates to formatting them for user-friendly displays, this object offers versatile methods and properties. By understanding its syntax, methods, and properties, you can handle any date-related task with ease.
You can also check out our other blogs on Code360.