Date.prototype.getUTCDate()

Returns the day of the month of a date according to universal time, from 1 to 31.

Signature
getUTCDate()
  • Added in JavaScript ES1

Return value

numberAn integer between 1 and 31 representing the day of the month for the given date according to universal time. Returns NaN if the date is invalid.

getUTCDate() returns the day-of-month number as it appears in UTC — 1 through 31. Unlike months, it is one-based, matching what you would write on a calendar.

Do not confuse it with getUTCDay()

The names are one letter apart and do completely different things. getUTCDate() gives the day of the month; getUTCDay() gives the day of the week, zero-based from Sunday. Picking the wrong one is a silent bug, because both return plausible small integers.

When to reach for it

  • Assembling a date string by hand together with getUTCFullYear() and getUTCMonth().
  • Checking for a specific day, such as the first of the month for a billing run.
  • Comparing two instants for "same calendar day" without local time shifting the answer.

What trips people up

The day rolls over at midnight UTC, not at the viewer's midnight. For someone in UTC+10, getUTCDate() reports yesterday's number for the first ten hours of their day, and a UI that mixes it with local formatting will show a date that disagrees with itself. Pick UTC or local per feature and stay there.

Because the value is capped at the real length of the month, it never returns 31 for a 30-day month — but the setter happily accepts 32 and rolls into the next month.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Build the DD segment of a hand-formatted date string
  • Trigger a monthly job only when the UTC day is 1
  • Test whether two timestamps fall on the same UTC calendar day
  • Render a calendar grid cell number from a stored timestamp

Frequently asked questions

What is the difference between getUTCDate() and getUTCDay()?

getUTCDate() returns the day of the month from 1 to 31, while getUTCDay() returns the day of the week from 0 (Sunday) to 6 (Saturday). Both return small integers, so mixing them up produces wrong output without any error.

Why is getUTCDate() one day off from what my users see?

Because the UTC day rolls over at midnight UTC, not at their local midnight. Anyone whose offset pushes them across that boundary sees a different calendar day — use getDate() if the number must match the viewer's own calendar.

How do I get the last day of the month?

Create a date for the first of the next month and call setDate(0), which rolls back to the final day of the previous month; reading getDate() then gives 28, 29, 30 or 31. There is no getter that reports month length directly.

More examples

Same UTC calendar day?

Comparing all three UTC components is the reliable same-day test; the two instants 23 hours apart are the same day, while the two only two minutes apart are not. Subtracting timestamps and dividing by 86400000 would get both of these wrong.

Example · javascript
function sameUTCDay(a, b) {
  return (
    a.getUTCFullYear() === b.getUTCFullYear() &&
    a.getUTCMonth() === b.getUTCMonth() &&
    a.getUTCDate() === b.getUTCDate()
  );
}

const morning = new Date("2024-05-17T01:00:00Z");
const evening = new Date("2024-05-17T23:59:00Z");
const nextDay = new Date("2024-05-18T00:01:00Z");

console.log(sameUTCDay(morning, evening)); // true
console.log(sameUTCDay(evening, nextDay)); // false

Hand-format a UTC date string

The first two logs are identical, confirming the manual assembly matches the ISO output. The month needs +1 and padding; the day needs padding only, since it is already one-based.

Example · javascript
function utcStamp(d) {
  const p = (n) => String(n).padStart(2, "0");
  return `${d.getUTCFullYear()}-${p(d.getUTCMonth() + 1)}-${p(d.getUTCDate())}`;
}

const d = new Date("2024-03-09T22:30:00Z");
console.log(utcStamp(d));
console.log(d.toISOString().slice(0, 10));
console.log(utcStamp(new Date("2024-12-01T00:00:00Z")));

Discussion

  • Be the first to comment on this lesson.