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:

date.getUTCDate();

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

const date = new Date('2024-10-22T15:30:00Z'); // October 22, 2024, at 15:30:00 UTC const utcDate = date.getUTCDate(); console.log(utcDate);

Output:

22

Explanation:

  • The Date object represents October 22, 2024, at 15:30:00 UTC. The getUTCDate() method returns 22, indicating that it is the 22nd day of the month in UTC.

Example 2: Getting UTC Date for a Local Time

const localDate = new Date('2024-10-22T12:00:00'); // October 22, 2024, at 12:00 PM local time const utcDateFromLocal = localDate.getUTCDate(); console.log(utcDateFromLocal);

Output (depends on local timezone):

22

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 return 22. 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 return 21.

Example 3: Getting UTC Date for a Date Near Midnight

const nearMidnightLocal = new Date('2024-10-22T23:59:59'); // October 22, 2024, at 23:59:59 local time const utcDateNearMidnight = nearMidnightLocal.getUTCDate(); console.log(utcDateNearMidnight);

Output (depends on local timezone):

23

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 return 23.

Example 4: Getting UTC Date for a Date Object Created with UTC

const utcDate = new Date(Date.UTC(2024, 9, 22)); // October is month 9 (0-indexed) const utcDateCreated = utcDate.getUTCDate(); console.log(utcDateCreated);

Output:

22

Explanation:

  • The Date.UTC() method creates a date object for October 22, 2024, in UTC. The getUTCDate() method will return 22 since this date is indeed the 22nd of October in UTC.

Summary:

  • date.getUTCDate() returns the day of the month for a Date 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.