JavaScript Date getUTCDate()
Syntax & Examples
getUTCDate() method
The getUTCDate() method of the Date object in JavaScript returns the day (date) of the month (1 – 31) in the specified date according to universal time.
Syntax of getUTCDate()
The syntax of Date.getUTCDate() method is:
getUTCDate()
This getUTCDate() method of Date returns the day (date) of the month (1 – 31) in the specified date according to universal time.
Return Type
Date.getUTCDate() returns value of type Number
.
✐ Examples
1 Using getUTCDate() to get the UTC date of a specific date
In JavaScript, we can use the getUTCDate()
method to get the day of the month from a Date object according to universal time.
For example,
- We create a new Date object
date
representing January 15, 2021. - We use the
getUTCDate()
method to get the day of the month fromdate
. - The day of the month is stored in the variable
utcDate
. - We log
utcDate
to the console usingconsole.log()
method.
JavaScript Program
const date = new Date('2021-01-15');
const utcDate = date.getUTCDate();
console.log(utcDate);
Output
15
2 Using getUTCDate() with the current date
In JavaScript, we can use the getUTCDate()
method to get the current day of the month from the current date according to universal time.
For example,
- We create a new Date object
now
representing the current date and time. - We use the
getUTCDate()
method to get the current day of the month fromnow
. - The current day of the month is stored in the variable
currentUTCDate
. - We log
currentUTCDate
to the console usingconsole.log()
method.
JavaScript Program
const now = new Date();
const currentUTCDate = now.getUTCDate();
console.log(currentUTCDate);
Output
31
3 Using getUTCDate() with a specific date in the past
In JavaScript, we can use the getUTCDate()
method to get the day of the month from a specific date in the past according to universal time.
For example,
- We create a new Date object
pastDate
representing July 4, 1776. - We use the
getUTCDate()
method to get the day of the month frompastDate
. - The day of the month is stored in the variable
pastUTCDate
. - We log
pastUTCDate
to the console usingconsole.log()
method.
JavaScript Program
const pastDate = new Date('1776-07-04');
const pastUTCDate = pastDate.getUTCDate();
console.log(pastUTCDate);
Output
4
Summary
In this JavaScript tutorial, we learned about getUTCDate() method of Date: the syntax and few working examples with output and detailed explanation for each example.