JavaScript date.getUTCDate() method
The date.getUTCDate()
method in JavaScript returns the day of the month (from 1 to 31) for a specified Date
object, according to Universal Coordinated Time (UTC). This method is useful for obtaining the day of the month without regard to local time zone differences.
Syntax:
Returns:
- An integer between 1 and 31 that represents the day of the month in UTC.
Example 1: Getting UTC Date for a Specific Date
Output:
Explanation:
- The
Date
object represents October 22, 2024, at 15:30:00 UTC. ThegetUTCDate()
method returns22
, indicating that it is the 22nd day of the month in UTC.
Example 2: Getting UTC Date for a Local Time
Output (depends on local timezone):
Explanation:
- If you are in a timezone that is UTC-3, the local time of October 22, 2024, at 12:00 PM is still the 22nd in UTC. The
getUTCDate()
method will return22
. However, if the local time were set such that it crosses over into the next day in UTC (like a UTC-8 timezone at 9 PM), it would return21
.
Example 3: Getting UTC Date for a Date Near Midnight
Output (depends on local timezone):
Explanation:
- If you are in a timezone that is UTC-3, the UTC equivalent of October 22, 2024, at 23:59:59 local time would be October 23, 2024, at 02:59:59 UTC. Thus,
getUTCDate()
would return23
.
Example 4: Getting UTC Date for a Date Object Created with UTC
Output:
Explanation:
- The
Date.UTC()
method creates a date object for October 22, 2024, in UTC. ThegetUTCDate()
method will return22
since this date is indeed the 22nd of October in UTC.
Summary:
date.getUTCDate()
returns the day of the month for aDate
object in UTC, ranging from 1 to 31.- It is useful for retrieving the day without local time zone interference, making it valuable for applications requiring consistent date handling across different time zones.