In JavaScript, the Date object is a built-in object that allows you to work with dates and times. It provides a variety of methods to handle dates, compare them, manipulate them, and format them.
The Date object in JavaScript represents a single point in time. It provides various functionalities to work with dates and times like creating a Date object in several ways, and it allows you to perform operations like extracting parts of the date (year, month, day), comparing dates, and manipulating dates.
Creating a Date Object
You can create a Date object in multiple ways. Below are some of the most common methods:
1. Using the Default Date() Constructor
The default constructor creates a Date object representing the current date and time.
JavaScript
const now = new Date();
console.log(now);
Output2025-01-16T04:25:58.204Z
- new Date() creates a Date object for the current date and time.
- console.log(now) prints the current date and time to the console.
2. Using a specific date string
You can pass a date string to the constructor to create a Date object for a specific date and time.
JavaScript
const specificDate = new Date("2023-12-25T10:00:00");
console.log(specificDate);
Output2023-12-25T10:00:00.000Z
- new Date("2023-12-25T10:00:00") creates a date for December 25, 2023, at 10:00 AM.
- console.log(specificDate) shows the date and time: "2023-12-25T10:00:00".
3. Using Year, Month, and Other Parameters
The Date constructor can also take individual parameters for year, month (0-indexed), day, hour, minute, second, and millisecond.
JavaScript
const anotherDate = new Date(2023, 11, 25, 10, 0, 0);
console.log(anotherDate);
Output2023-12-25T10:00:00.000Z
- new Date(2023, 11, 25, 10, 0, 0) creates a Date object for December 25, 2023, at 10:00 AM (months are 0-indexed, so 11 represents December).
- console.log(anotherDate) outputs the specified date and time: "2023-12-25T10:00:00.000".
4. Using Timestamps
You can create a Date object from a timestamp (milliseconds since January 1, 1970).
JavaScript
const timestampDate = new Date(1672531200000);
console.log(timestampDate);
Output2023-01-01T00:00:00.000Z
- new Date(1672531200000) creates a Date object based on the number of milliseconds since January 1, 1970 (Unix Epoch).
- console.log(timestampDate) outputs the corresponding date and time for that timestamp, which is "2023-01-01T00:00:00.000Z".
Getting Date and Time Components
Once you have a Date object, you can easily extract various components like the year, month, day, hours, minutes, and seconds.
JavaScript
let cDate = new Date();
console.log(cDate.getFullYear()); // Gets the current year
console.log(cDate.getMonth()); // Gets the current month (0-indexed)
console.log(cDate.getDate()); // Gets the current day of the month
console.log(cDate.getHours()); // Gets the current hour
console.log(cDate.getMinutes()); // Gets the current minutes
console.log(cDate.getSeconds()); // Gets the current seconds
Note: The getMonth() method returns a zero-indexed value (0 for January, 11 for December).
Date Manipulation
You can perform various date manipulations such as adding or subtracting days, months, or years to/from a specific date.
Adding Days to a Date
JavaScript
let cDate = new Date();
cDate.setDate(cDate.getDate() + 5); // Adds 5 days to the current date
console.log(cDate);
Output2025-02-16T18:36:36.850Z
In this example, we use the setDate() method to add 5 days to the current date.
Comparing Dates
JavaScript also allows you to compare two Date objects to check if they are equal or which one is earlier or later.
JavaScript
let date1 = new Date('2023-12-31');
let date2 = new Date('2024-01-01');
if (date1 < date2) {
console.log('date1 is earlier than date2');
} else if (date1 > date2) {
console.log('date1 is later than date2');
} else {
console.log('Both dates are the same');
}
Outputdate1 is earlier than date2
In this example, we compare date1 and date2 using comparison operators like <, >, and ===.
Formatting Dates
JavaScript provides various methods to format dates into a human-readable string, although formatting options can be limited in plain JavaScript. You can use toLocaleDateString() or toLocaleTimeString() to format dates and times.
Formatting a Date to a Local String
JavaScript
let now = new Date();
let formattedDate = now.toLocaleDateString('en-US');
console.log(formattedDate); // Output format: MM/DD/YYYY
The toLocaleDateString() method allows you to specify a locale (e.g., 'en-US' for American English) and formats the date accordingly.
Formatting Date and Time
JavaScript
let now = new Date();
let formattedDateTime = now.toLocaleString('en-US');
console.log(formattedDateTime); // Output format: MM/DD/YYYY, HH:MM:SS AM/PM
Output2/11/2025, 6:41:41 PM
The toLocaleString() method returns both the date and time in a localized format.
UTC Methods
In JavaScript, Date objects are based on the local time zone. However, you can use the UTC methods to handle date and time in Coordinated Universal Time (UTC).
JavaScript
let now = new Date();
console.log(now.getUTCFullYear()); // Gets the current year in UTC
console.log(now.getUTCMonth()); // Gets the current month (0-indexed) in UTC
console.log(now.getUTCDate()); // Gets the current day of the month in UTC
The getUTC*() methods return the corresponding date components in UTC.
Date Parsing
JavaScript's Date object allows you to parse date strings into Date objects.
JavaScript
let dateS = '2023-12-31';
let parsed = new Date(dateS);
console.log(parsed);
Output2023-12-31T00:00:00.000Z
You can also parse date strings in various formats such as 'YYYY-MM-DD', 'MM/DD/YYYY', or even ISO 8601 date strings.
Date Object Method's Table
Here are some methods that define the usage of a Date object, These are non-static methods.
Below methods are returns all the values according to local time
Method | Description |
---|
Date() | It returns presents day’s date and time. |
getDate() | It returns the day for the specified date. |
getDay() | It returns the day of the week for the specified date. |
getFullYear() | It returns the year of the specified date. |
getYear() | This method returns the year in the specified date. |
getHours() | It returns the hour in a specified date. |
getMilliseconds() | It returns the milliseconds in the specified date. |
getMinutes() | It returns the minutes in the specified date. |
getMonth() | It returns the month in the specified date. This also find the month. |
getSeconds() | This method returns the seconds in the specified date. |
getTime() | This method returns the date in terms of numeric value as milliseconds. |
setDate() | This method sets the day of the month for a specified date. |
setFullYear() | This method sets the full year for a specified date. |
Below methods are returns all the values according to universal time
Conclusion
The JavaScript Date object is a powerful tool for working with dates and times. By understanding its methods and properties, you can handle a wide variety of date-related tasks in your applications. For more advanced date manipulation and formatting, consider using external libraries to complement the built-in functionality.
Similar Reads
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
JavaScript Date Reference
The JavaScript Date object is important for handling dates and times in web development. It provides various methods to create, manipulate, and format dates. Understanding the Date object and its methods allows you to work effectively with dates and times in your applications. This guide covers the
4 min read
JavaScript Date setHours() Method
The JavaScript date.setHours() method is used to set hours into a date object which is created using the Date() constructor.SyntaxDateObj.setHours(hours_Value)Parameters hours_Value: This parameter holds the value of hour which is used to set in the Date() constructor.Return ValueIt returns the new
4 min read
JavaScript Date UTC() Method
In JavaScript, the Date.UTC() method is used to create a date object representing a specified date and time in UTC (Coordinated Universal Time). It accepts the year, month, day, hour, minute, second, and millisecond components of the date and returns the number of milliseconds since January 1, 1970,
4 min read
JavaScript Date toJSON() Method
JavaScript date.toJSON() method converts the given date objectâs contents into a string. The date object is created using the date() constructor.Syntax:dateObj.toJSON()Parameters: This method does not accept any parameter. It is just used along with a Date object created using the Date() constructor
4 min read
JavaScript Date setDate() Method
The setDate() method in JavaScript is used to set the day of the month for a specified date object, according to local time. It allows you to update the day part of the date without changing other parts like month and year. Date setDate() SyntaxdateObj.setDate(date_Value);Date setDate() ParameterThi
2 min read
JavaScript Date setSeconds() Method
The date.setSeconds() method is used to set seconds into a Date object which is created using Date() constructor. Syntax: DateObj.setSeconds(seconds_Value) Parameter: This method accepts a single parameter as mentioned above and described below: seconds_Value: This parameter holds the value of secon
4 min read
JavaScript Date setUTCDate() Method
The date.setUTCDate() method is used to set the date of a month according to universal time into a date object which is created using the Date() constructor. Syntax: DateObj.setUTCDate(date_Value); Parameter: This method accepts a single parameter as mentioned above and described below: date_Value T
5 min read
JavaScript Date toString() Method
JavaScript date.toString() method is used to convert the given date object's contents into a string. The date object is created using the date() constructor. Syntax:dateObj.toString();Parameters:This method does not accept any parameter. It is just used along with a Date object created using the Dat
3 min read
JavaScript Date setUTCHours() Method
The date.setUTCHours() method is used to set hours into a date object according to universal time which is created using the Date() constructor.Syntax: DateObj.setUTCHours(hours_Value); Parameter: This method accepts a single parameter as mentioned above and described below: hours_Value: This parame
4 min read