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


Similar Reads

How to validate if input date (end date) in input field must be after a given date (start date) using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In certain cases, we want the user to type a date that must come after some given date(Ex. 'end date' must be after 'start date')
5 min read
How to validate if input date (start date) in input field must be before a given date (end date) using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In certain cases, we want the user to type a date that must come before some given date (Ex: 'start date' must come before 'end da
5 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 day).setHours(): It is used to set the hour (0-23).
2 min read
How to check the input date is equal to today's date or not using JavaScript ?
Given a date object and the task is to determine whether the given date is the same as today's date or not with the help of JavaScript. There are two well-famous approaches are discussed here. Approach 1: Get the input date from user (var inpDate) and the today's date by new Date(). Now, use .setHours() method on both dates by passing parameters of
3 min read
How to convert UTC date time into local date time using JavaScript ?
Given an UTC date and the task is to convert UTC date time into local date-time using JavaScript toLocaleString() function. Syntax: var theDate = new Date(Date.parse('DATE_IN_UTC')) theDate.toLocaleString() Example 1: This example converts UTC date time into local date time using JavaScript. C/C++ Code <body> <h1 style="color:green;
2 min read
How to get all the methods of an object using JavaScript ?
In this article, we will learn how to get all the methods of an object using JavaScript. In JavaScript, we can get all the methods of an object by iterating over each object and checking if its property value is a function. An HTML document contains some methods and the task is to get all methods of the object. There are two methods to solve this p
2 min read
How to get the current date in JavaScript ?
In this article, we will see how to get the current date using the built-in methods in JavaScript & will see their implementation through the examples. There are 2 methods to perform this task: Using Date().toDateString() MethodUsing their respective method for fetching day, month, and year.There are several other ways to perform this task but
3 min read
How to get the first and last date of current month using JavaScript ?
Given a month and the task is to determine the first and last day of that month in proper format using JavaScript. JavaScript getDate() Method: This method returns the day of the month (from 1 to 31) for the defined date. Syntax: Date.getDate() Parameters: This method does not accept any parameters. Return value: It returns a number from 1 to 31, r
3 min read
How to get the month name from Date using JavaScript ?
The purpose of this article is to get the month name from a particular date using JavaScript getMonth() method. This method is used to fetch the month(0 to 11) from the given Date object. It returns an integer value ranging from (0 to 11) Zero (0) means January, 1 means February, and so on, till 11 means December. Syntax: DateObj.getMonth() Example
1 min read
How to get tomorrow's date in a string format in JavaScript ?
In this article, we will see how to print tomorrow's date in string representation using JavaScript. To achieve this, we use the Date object and create an instance of it. After that by using the setDate() method, we increase one date to the present date. Now by using the getDate() method you will get tomorrow's date. Now to convert that date to the
2 min read