Table of contents
To create and manipulate a particular date, we will have to create a date type object which is a built-in object in JavaScript.
To learn how to create a date type object check the following tutorial: JavaScript Date: Create Dates in JavaScript
The Date object has many methods that can be classified into different groups: the methods that allow us to obtain a date, those that allow us to define a date, others that allow us to get the current time, etc.
In this tutorial we will go through different methods to get the date, month, year, time, minutes, seconds…So the methods we’re going to use are called “getters” because they all start with “get“.
getFullYear() Method
The getFullYear()
method returns the complete year of a date.
getMonth() Method
The getMonth()
method returns the month of a date. This month is returned as a number.
Here the number of the month returned is 2. It does not mean that the month is February, but 2 represents the month’s index.
Because if we were to add all the months from January to December to an array, then the index of January would be 0, and the index of December will be 11.
So here the getMonth()
method returned 2 as an index for the month of March.
In general, getMonth()
method is used to return the current month.
getDate() Method
The getDate()
method returns the day of a date. The day is returned as a number.
getHours() Method
The getHours()
method returns the hours of a date. The hours are between 0 and 23.
getMinutes() Method
The getMinutes()
method returns the minutes of a date. The minutes are between 0 and 59.
getSeconds() Method
The getSeconds()
method returns the seconds of a date. The seconds are also between 0 and 59.
getDay() Method
The getDay()
method returns the day of a date (day of month or day of week). The day is returned as a number and this number is between 0 and 6.
The number returned here is 4. 4 refers to Thursday.
If we add all the days of the week to an array like this : [“Sunday”,”Monday”,”Tuesday”,”Wednesday”,”Thursday”,”Friday”,”Saturday”]
the index 0 will refer to Sunday, and that is because in JavaScript, the first day of the week is Sunday. So index 1 represents Monday, 2 represents Tuesday etc…
getTime() Method
The getTime()
method returns the number of milliseconds since January 1, 1970.
getUTCFullYear() Method
The getUTCFullYear()
returns the complete UTC year of a date. The difference between getFullYear()
and getUTCFullYear()
methods is around new Year time. If the day date is December 31 or January 1, the return value of getUTCFullYear()
and getFullYear()
may be different as the getUTCFullYear()
returns the Year according to the universal time.
getUTCMonth() Method
The getUTCMonth()
returns the UTC Month.
getUTCDate() Method
The getUTCDate()
returns the UTC Date.
getUTCDay() Method
The getUTCDay()
returns the UTC Day as a number. This number is between 0 and 6.
The 22nd of December is a Thursday. Here the number returned is 4, and 4 refers to Thursday as explained in the getDay()
method.
getUTCHours() Method
The getUTCHours()
returns the UTC Hour. If you use getHours()
instead getUTCHours()
method, you will get the local time that is on your computer. Because getHours()
returns the local time.
However if you use the getUTCHours()
, you will get the UTC time instead.
The UTC time might be different from the local time due to the time difference.
So here the UTC time is 3PM while on my computer I have 4 PM. So my time zone is UTC+1.
getUTCMinutes() Method
The getUTCMinutes()
returns UTC minutes.
getUTCSeconds() Method
The getUTCSeconds()
returns UTC seconds.
Conclusion
To retrieve date properties like hours, minutes, seconds, time… the Date() object allows us to do that through the get methods.
The following get methods get back the local hours, minutes, seconds, day, time…:
getDate()
getFullYear()
getMonth()
getDay()
getHours()
getMinutes()
getSeconds()
The following get methods get back the UTC (Universal time and date) hours, minutes, seconds, day, time…:
getUTCDate()
getUTCFullYear()
getUTCMonth()
getUTCDay()
getUTCHours()
getUTCMinutes()
getUTCSeconds()
Read More
JavaScript Date: Create Dates in JavaScript
5 ways to declare a function in JavaScript
How to loop through an array in JavaScript?
Understanding JavaScript Objects: From Basic Concepts to Practical Usage
Object constructor functions: How does the Javascript constructor work?