Open In App

JavaScript Get Date Methods

Last Updated : 12 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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, seconds, and milliseconds from a Date Object.

The following is the list of the date method to retrieve the various dates from the Date object in Javascript.

Methods:

  • getDate(): It is used to get the day as a number (1-31).
  • getFullYear(): It is used to get the year.
  • getHours(): It is used to get the hour (0-23).
  • getMilliseconds(): It is used to get the milliseconds (0-999).
  • getMinutes(): It is used to get the minutes (0-59).
  • getMonth(): It is used to get the month (0-11).
  • getSeconds(): It is used to get the seconds (0-59).
  • getTime(): It used to return the number of milliseconds since 1 January 1970.
  • getDay(): It is used to get the weekday as a number (0-6).
  • date.now(): It is used to return the number of milliseconds elapsed since January 1, 1970, 00:00:00 UTC.

We will implement the different date methods & understand their usage through the examples.

getHours() Method

The getHours() method in JavaScript is used to return the hours of a date as a number (0-23).

Example: This example describes the getHours() method for retrieving the hour for the specified date to local time from the date object.

JavaScript




let d = new Date();
console.log(d.getHours());


Output

4

getDate() Method

The getDate() method in JavaScript is used to return the day of a date as a number (1-31).

Example: This example describes the getDate() method for retrieving the current date of the month from the date object.

JavaScript




let d = new Date();
console.log(d.getDate());


Output

1

getMonth() Method

The getMonth() method in JavaScript is used to return the month of a date as a number (0-11).

Example: This example describes the getMonth() method for retrieving the month from the date object.

JavaScript




let d = new Date();
console.log(d.getMonth() + 1);


Output

8

In Javascript, the month number starts from 0 which denotes 1st month ie., January & ends with the month number 11 which denotes the last month ie., December. So, we need to add 1 to get the current month.

getFullYear() Method

The getFullYear() method in JavaScript is used to return the year (in 4-digit) for the specified date according to local time.

Example: This example describes the getFullYear() method for retrieving the year from the date object.

JavaScript




let d = new Date();
console.log(d.getFullYear());


Output

2023

getTime() Method

The getTime() method in JavaScript is used to return the number of milliseconds(0–999).

Example: This example describes the getTime() method for retrieving the number of milliseconds from the date object.

JavaScript




let d = new Date();
console.log(d.getTime());


Output

1690865918886

getSeconds() Method

The getSeconds() method in JavaScript returns the seconds of a date object (0-59).

Example: This example describes the getSeconds() method for retrieving the seconds from the date object.

JavaScript




let d = new Date();
console.log(d.getSeconds());


Output

38

Supported Browsers:

  • Google Chrome 1.0
  • Firefox 1.0
  • Microsoft Edge 12.0
  • Opera 3.0
  • Safari 1.0


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads