JavaScript Get Date Methods
Last Updated :
12 Jan, 2024
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.
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());
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());
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);
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.
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());
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());
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());
Supported Browsers:
- Google Chrome 1.0
- Firefox 1.0
- Microsoft Edge 12.0
- Opera 3.0
- Safari 1.0
Similar Reads
Java DateFormat getInstance() Method with Examples The getInstance() method of DateFormat class will return the date and time formatter with the default formatting style for the default locale. Syntax: public static final DateFormat getInstance() Parameter: This method doesn't require any parameters. Return value: This method will return the date fo
2 min read
java.time.LocalDate Class in Java Java is the most popular programming language and widely used programming language. Java is used in all kind of application like as mobile application, desktop application, web application. In this Java java.time.LocalDate class is imported which represents to display the current date. java.time: It
4 min read
DateFormat Class in Java The java.text.DateFormat is an abstract class that is used to format and parse dates for any locale. It allows us to format date to text and parse text to date. DateFormat class provides many functionalities to obtain, format, parse default date/time. DateFormat class extends Format class that means
4 min read
Calendar Class in Java with examples Calendar class in Java is an abstract class that provides methods for converting date between a specific instant in time and a set of calendar fields such as MONTH, YEAR, HOUR, etc. It inherits Object class and implements the Comparable, Serializable, Cloneable interfaces. As it is an Abstract class
4 min read
Java Program to Get Today's Date Java is the most powerful programming language, by which we can perform many tasks and Java is an industry preferable language. So it is filled with a huge amount of features. Here we are going to discuss one of the best features of Java, which is how to get today's or current date using Java. Metho
2 min read