Date.prototype.getUTCMonth()

Returns the zero-based month of a date according to universal time, 0 for January through 11 for December.

Signature
getUTCMonth()
  • Added in JavaScript ES1

Return value

numberAn integer between 0 and 11 representing the month for the given date according to universal time, where 0 is January and 11 is December. Returns NaN if the date is invalid.

getUTCMonth() returns the month as it appears in UTC, numbered from zero. January is 0, December is 11. This is the single most common source of off-by-one bugs in JavaScript date code, and it exists because the same numbering is what the Date constructor and setMonth() accept.

When to reach for it

  • Indexing directly into an array of month names or labels — the zero base is exactly right there.
  • Deriving a quarter: Math.floor(d.getUTCMonth() / 3) + 1.
  • Grouping records by month in a way that does not shift for users in other time zones.

What trips people up

Add 1 whenever the number is shown to a person or written into an ISO-style string, and remember to pad it: String(m + 1).padStart(2, "0"). Forgetting the + 1 is what turns March into February in a hand-built date string.

The UTC/local split bites around month boundaries. An instant at 2024-06-30T23:00:00Z reports month 5 (June) from getUTCMonth() but 6 (July) from getMonth() anywhere east of Greenwich. For anything stored, exported, or compared across machines, stay on the UTC getters throughout.

Example

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

When to use it

  • Look up a month name from a labels array without arithmetic
  • Compute the calendar quarter a timestamp falls in
  • Group log entries into monthly buckets on a UTC server
  • Build the MM segment of a hand-formatted date string

Frequently asked questions

Why does getUTCMonth() return 6 for July?

Because months are zero-based in JavaScript: January is 0 and December is 11. Add 1 whenever you display the number, but keep it zero-based when passing it to the Date constructor, setMonth(), or a month-name array.

How do I get the month name from a Date?

Index a names array with getUTCMonth() directly, or use Intl.DateTimeFormat with { month: "long", timeZone: "UTC" } for localized names. The zero-based index is already the correct array position, so no adjustment is needed.

What is the difference between getMonth() and getUTCMonth()?

getMonth() reads the month in the local time zone and getUTCMonth() reads it in UTC. They differ for instants within a few hours of a month boundary, so use the UTC form for anything stored or shared between machines.

More examples

UTC and local months disagree at the boundary

Both strings say "December 31" but carry opposite offsets, so in UTC one is still December and the other has rolled into January. The ISO strings show why the month indexes differ.

Example · javascript
const d1 = new Date("1975-12-31T23:15:30+11:00");
const d2 = new Date("1975-12-31T23:15:30-11:00");

console.log(d1.getUTCMonth()); // 11 (December)
console.log(d2.getUTCMonth()); // 0  (January)
console.log(d1.toISOString(), d2.toISOString());

Quarter and monthly buckets

The quarter formula only works because the month is zero-based — dividing by 3 puts January, February and March all in quarter 1. The bucket key adds 1 and pads, which is the display form.

Example · javascript
const quarter = (d) => Math.floor(d.getUTCMonth() / 3) + 1;
console.log(quarter(new Date("2024-11-15T00:00:00Z"))); // 4
console.log(quarter(new Date("2024-01-02T00:00:00Z"))); // 1

const stamps = ["2024-01-05", "2024-01-30", "2024-02-11"];
const buckets = {};
for (const s of stamps) {
  const d = new Date(s);
  const key = `${d.getUTCFullYear()}-${String(d.getUTCMonth() + 1).padStart(2, "0")}`;
  buckets[key] = (buckets[key] ?? 0) + 1;
}
console.log(buckets); // { '2024-01': 2, '2024-02': 1 }

Discussion

  • Be the first to comment on this lesson.