The Wayback Machine - https://web.archive.org/web/20250114155121/https://www.geeksforgeeks.org/javascript-date-objects/
Open In App

JavaScript Date Objects

Last Updated : 08 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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);

Output
2024-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);

Output
Today'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);

Output
Date 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);

Output
2014-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:

MethodDescription
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:

MethodsDescription
getUTCDate()It returns the day of a month for a specified date.
getUTCDay()It returns the day of the week for a specified date.
getUTCFullYear()This method returns the year for a specified date.
getUTCHours()It returns the hours in a specified date.
getUTCMilliseconds()This method returns the milliseconds form for a specified date.
getUTCMinutes()This method returns the minutes in a specified date.
getUTCMonth()This method returns the month for a specified date.

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!


Next Article

Similar Reads

three90RightbarBannerImg