JavaScript Date getter methods for normal date and UTC date

Introduction :

In Javascript, the Date object is used to work with date and time. It provides a lot of different methods to create Date and to do different modification on it. It provides a set of different getter methods to get different date properties. In this post, I will show you these getter methods with examples.

Javascript Date getter methods :

Following are the available getter methods available in Date :

1. getDate() :

It returns the current date of the month for a date object. This value ranges from 1 to 31.

2. getDay() :

It returns the day in a week. It ranges from 0 to 6. 0 is for Sunday and 6 is for Saturday.

3. getFullYear() :

It returns the full year, i.e. in four digits for a Date object.

4. getHours :

Get the current hour in 24 hour format. It is in the range from 0 to 23.

5. getMilliseconds() :

Return the milliseconds for the current Date object. It is in the range from 0 to 999.

6. getMinutes() :

Return the current minutes for the current Date object. It is in the range from 0 to 59.

7. getSeconds() :

Get the current seconds in the range of 0 to 59.

8. getMonth() :

Get the current month in the range of 0 to 11.

9. getTime() :

It returns the time since January 1, 1970, 00:00:00 UTC in milliseconds i.e. time passed in milliseconds starting January 1, 1970.

10. getTimezoneOffset() :

Returns the timezone offset for the Date object in minutes.

11. getYear() :

Get the current year in two or three digits format.

Example :

Let’s learn the above methods with an example :

var date = new Date();

console.log(`Given date : ${date}`);

console.log(`getDate() : ${date.getDate()}`);

console.log(`getDay() : ${date.getDay()}`);

console.log(`getFullYear() : ${date.getFullYear()}`);

console.log(`getHours() : ${date.getHours()}`);

console.log(`getMilliseconds() : ${date.getMilliseconds()}`);

console.log(`getMinutes() : ${date.getMinutes()}`);

console.log(`getMonth() : ${date.getMonth()}`);

console.log(`getSeconds() : ${date.getSeconds()}`);

console.log(`getTime() : ${date.getTime()}`);

console.log(`getTimezoneOffset() : ${date.getTimezoneOffset()}`);

console.log(`getYear() : ${date.getYear()}`);

Output :

Given date : Thu Sep 26 2019 19:23:09 GMT-0500 (Eastern Standard Time)
getDate() : 26
getDay() : 4
getFullYear() : 2019
getHours() : 19
getMilliseconds() : 920
getMinutes() : 23
getMonth() : 8
getSeconds() : 9
getTime() : 1569505989920
getTimezoneOffset() : 300
getYear() : 119

UTC methods :

Date also has few methods that returns the data in UTC. Let’s have a look :

1. getUTCDate() :

This method is used to get the date in UTC format.

2. getUTCDay() :

Returns the day of the week as per UTC in range 0 to 6.

3. getUTCFullYear() :

Returns the full year as per UTC in four digit.

4. getUTCHours() :

Get the current hours as per UTC. This is in range 0 to 23.

5. getUTCMilliseconds() :

Get the current milliseconds time as per UTC. It is in range 0 to 999.

6. getUTCMinutes() :

Get the current minute as per UTC. It is in range 0 to 59.

7. getUTCMonth() :

Get the current month as per UTC in range 0 to 11.

8. getUTCSeconds() :

Get the seconds in UTC in the range 0 to 59.

Example :

You can run the below example to check these methods :

var date = new Date();

console.log(`Given date : ${date}`);

console.log(`getUTCDate() : ${date.getUTCDate()}`);

console.log(`getUTCDay() : ${date.getUTCDay()}`);

console.log(`getUTCFullYear() : ${date.getUTCFullYear()}`);

console.log(`getUTCHours() : ${date.getUTCHours()}`);

console.log(`getUTCMilliseconds() : ${date.getUTCMilliseconds()}`);

console.log(`getUTCMinutes() : ${date.getUTCMinutes()}`);

console.log(`getUTCMonth() : ${date.getUTCMonth()}`);

console.log(`getUTCSeconds() : ${date.getUTCSeconds()}`);