The Date Object in JavaScript helps you work with dates and times. A date can be created using the new Date(). It can handle dates and times with very precise milliseconds and can represent dates up to 100 million days from January 1, 1970, all the way to the year 275755.
With the Date object, you can easily get and set parts of the date, like the year, month, day, hour, minute, second, and millisecond, in local or UTC/GMT time. There are four main ways to create a Date object, but the most common is using a new Date().
Syntax:
new Date()
new Date(milliseconds)
new Date(dataString)
new Date(year, month, date, hour, minute, second, millisecond)
new Date()
The Date() constructor creates a Date object which sets the current date and time depend on the browser’s time zone. It does not accepts any value.
Example: This example creates a new date objects and prints the date on the console.
JavaScript
// "newDate" is a Date object
let newDate = new Date();
// Prints today's date to the console
console.log(newDate);
Output2024-10-08T07:42:51.861Z
new Date(milliseconds)
This method accepts single parameter milliseconds which indicates any numeric value. This argument is taken as the internal numeric representation of the date in milliseconds.
Example: In this example we pass a parameter in milliseconds.
JavaScript
// "newDate" is a Date object
let newDate = new Date(4500);
// Prints the date to the console
console.log("Today's date: " + newDate);
OutputToday's date: Thu Jan 01 1970 00:00:04 GMT+0000 (Coordinated Universal Time)
new Date(datastring)
This method accepts single parameter dataString which indicates any string value. It is a string representation of a date and return the datastring with the day.
Example: Here we add a string value as a parameter.
JavaScript
// "newDate" is a Date object
let newDate = new Date("October 13, 2013 11:13:00");
// Prints the date string to the console
console.log("Date string with day: " + newDate);
OutputDate string with day: Sun Oct 13 2013 11:13:00 GMT+0000 (Coordinated Universal Time)
new Date(year, month, date, hour, minute, second, millisecond)
This method accepts seven parameters as mentioned above and described below:
Example: This example shows the implementation of the above approach.
JavaScript
// "newDate" is a Date object
let newDate = new Date(2014, 10, 24, 10, 33, 30, 0);
// Prints the date to the console
console.log(newDate);
Output2014-11-24T10:33:30.000Z
- year: Integer value which represents the year. This should always specify the year in full, i.e. use 2018, instead of use 18.
- month: Integer value which represents the month. The integer values are starts from 0 for January to 11 for December.
- date: Integer value which represents the date.
- hour: Integer value which represents the hour in 24-hours scale.
- minute: Integer value which represents the minute.
- second: Integer value which represents the second.
- millisecond: Integer value which represents the millisecond.
Properties of Date object:
- Prototype: Prototype allows us to add properties and methods to an object.
- Date constructor: It defines the function that creates an Date object’s prototype.
Date Object Methods:
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:
Become an expert in solving problems with DSA JavaScript—the course designed to teach you Data Structures and Algorithms using JavaScript. Over the next 90 days, dive deep into key DSA concepts, learn to optimize your code, and gain hands-on experience solving challenging problems. Whether you're a beginner or looking to refine your JavaScript skills, this course will provide the knowledge you need to succeed.
Take on the Three 90 Challenge today! Complete 90% of the course within 90 days, and you’ll earn a 90% refund. This challenge is your chance to stay motivated, improve your skills, and get rewarded for your progress. Don’t wait—start your journey to DSA mastery with JavaScript today!
Similar Reads
JavaScript Date Objects
The Date Object in JavaScript helps you work with dates and times. A date can be created using the new Date(). It can handle dates and times with very precise milliseconds and can represent dates up to 100 million days from January 1, 1970, all the way to the year 275755. With the Date object, you c
4 min read
JavaScript Date
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. What is the JavaScript
4 min read
JavaScript Set Date Methods
There are various methods to set the date in JavaScript. The data values can be set like years, months, days, hours, minutes, seconds, and milliseconds for a Date Object. Method: setDate(): It is used to set the day as a number (1-31).setFullYear(): It is used to set the year (optionally month and d
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
JavaScript Adding seconds to Date object
Given a date object and the task is to add seconds to the date using JavaScript. We will use a few methods to add seconds to the date, that are discussed below: JavaScript getSeconds() Method: This method returns the Seconds(from 0 to 59) of the provided date and time. Syntax: Date.getSeconds() Para
3 min read
JavaScript Date now() Method
The Date.now() method in JavaScript returns the current timestamp in milliseconds since January 1, 1970. This method doesn’t require creating a new date object, making it one of the fastest and most efficient ways to capture the current time in your code. Syntaxlet curr_date = Date.now();ParametersT
3 min read
JavaScript Subtract Days from Date Object
Given a date and the task is to subtract days from the date using JavaScript. To subtract days from date, some methods are used which are described below: JavaScript getDate() Method: This method returns the day of the month (from 1 to 31) for the defined date. Syntax: Date.getDate() Parameters: Thi
3 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 setYear() Method
JavaScript date.setYear() method is used to set the year of a particular date object. This method is different from other date methods as it accepts any two-digit number. It will accept the number as a number of years after 1900. It is a legacy method and has been deprecated in some newer browsers.
2 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 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 Adding minutes to Date object
Given a date and the task is to add minutes to it using JavaScript. To add minutes to the date object, some methods are used which are listed below: JavaScript getMinutes() Method: This method returns the Minutes (from 0 to 59) of the provided date and time. Syntax: Date.getMinutes() Parameters: Thi
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
JavaScript Date setUTCMonth() Method
The date.setUTCMonth() method is used to set month according to universal time into a date object which is created using the date() constructor. Syntax: dateObj.setUTCMonth(month_Value); Parameter: This method accepts a single parameter as mentioned above and described below: month_Value This parame
4 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 setMonth() Method
The date.setMonth() method is used to set month into a date object which is created using the Date() constructor. Syntax: DateObj.setMonth(month_Value); Parameter: This method accepts a single parameter as mentioned above and described below: month_Value: This parameter holds the value of the month
4 min read
JavaScript Date setUTCSeconds() Method
The date.setUTCSeconds() method is used to set seconds according to universal time into a date object which is created using the Date() constructor. Syntax: DateObj.setUTCSeconds(seconds_Value); Parameter: These methods accept a single parameter as mentioned above and described below: seconds_Value:
4 min read
JavaScript Date getDate() Method
The JavaScript getDate() method returns the day of the month (from 1 to 31) for a specified date according to local time. It's used with the Date object to extract and work with the day component of a date. Syntax:DateObj.getDate()Parameters:This method does not take any parameters.Return Value:Retu
4 min read
JavaScript Adding hours to the Date object
Given a date, the task is to add hours to it. To add hours to date in javascript, we're going to discuss a few techniques. First few methods to know. JavaScript getHours() Method: This method returns the hour (from 0 to 23) of the provided date and time. Syntax: Date.getHours() Parameters: This meth
3 min read