JavaScript Date: Get Date methods
Tags: javascript, beginners, webdev, tutorial
Published: 2022-12-26 17:27:23

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:[Create Dates in JavaScript](https://purpose-code.com/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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ekj4qftnfp6b4oe3v4gk.PNG)

## getMonth() Method

The **getMonth()** method returns the month of a date. This month is returned as a number.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aconausm1m4u3gtaomzw.PNG)

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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bp8uqw7kj2pn9poeg89h.PNG)

## getHours() Method

The **getHours()** method returns the hours of a date. The hours are between 0 and 23.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3vsr5tfceincrmh11bcq.PNG)

## getMinutes() Method

The **getMinutes()** method returns the minutes of a date. The minutes are between 0 and 59.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/11fx9sh4a0e0biuuxolr.PNG)

## getSeconds() Method

The **getSeconds()** method returns the seconds of a date. The seconds are also between 0 and 59.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oia3sommed0srm16g5od.PNG)

## 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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pi70za55lzzhgl5pb1nm.PNG)

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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/av3sr5xyls770sjzq1l3.PNG)

## 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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/buztk7ma0qbpac2wij4t.PNG)

## getUTCMonth() Method

The **getUTCMonth()** returns the UTC Month.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qhp8mqe6cnko91p8wct2.PNG)

## getUTCDate() Method

The **getUTCDate()** returns the UTC Date.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bzowyhom51mjvf6n1ui2.PNG)

## getUTCDay() Method

The **getUTCDay()** returns the UTC Day as a number. This number is between 0 and 6.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oeymbzrdser04ot6za3w.PNG)

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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5y6c5sl1olvxudnwh5o3.PNG)

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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6r07s6cqblrwk34jn9w9.PNG)

## getUTCSeconds() Method

The **getUTCSeconds()** returns UTC seconds.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0c6xcmzerv9pbrafvmv7.PNG)

## 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](https://purpose-code.com/javascript-date-create-dates-in-javascript/)

[5 ways to declare a function in JavaScript](https://purpose-code.com/5-ways-to-declare-a-function-in-javascript/)

[How to loop through an array in JavaScript?](https://purpose-code.com/how-to-loop-through-an-array-in-javascript/)

[Understanding JavaScript Objects: From Basic Concepts to Practical Usage](https://purpose-code.com/understanding-javascript-objects-from-basic-concepts-to-practical-usage/)

[Object constructor functions: How does the Javascript constructor work?](https://purpose-code.com/javascript-object-constructor-function/)

[Fetch API: Send data from front-end to back-end](https://purpose-code.com/fetch-api-send-data-from-front-end-to-back-end/)