Course contentsShow
← JavaScript
Lesson 1637 of 2,72335. Standard Library and Built-insFree lesson

getDate() vs getUTCDate()

Explore the difference between local and UTC getter methods, and when each set should be used.

getDate() vs getUTCDate()

You'll learn when to use local time getters versus their UTC counterparts, and why choosing the wrong one causes subtle bugs.

The Core Difference

JavaScript's Date object provides two sets of getter methods: local time methods (like getDate(), getHours(), getMonth()) and UTC methods (like getUTCDate(), getUTCHours(), getUTCMonth()). The local methods return values based on your system's timezone, while UTC methods always return values relative to Coordinated Universal Time (UTC), regardless of where you are.

Think of it like asking "What time is it?" If you ask someone in New York, they'll give you Eastern Time. If you ask for UTC time, you get the same universal reference no matter who you ask.

When to Use Each

Use local methods when:

  • Displaying dates/times to users in their own timezone
  • Working with dates that should reflect the user's local context
  • Building calendar interfaces or scheduling apps
const date = new Date('2024-01-15T14:30:00Z');
console.log(date.getDate());    // 15 (or 14, depending on your timezone!)
console.log(date.getHours());   // Varies by timezone

Use UTC methods when:

  • Storing or transmitting dates (APIs, databases)
  • Performing date calculations that must be timezone-independent
  • Working with dates where consistent behavior matters more than local display
const date = new Date('2024-01-15T14:30:00Z');
console.log(date.getUTCDate());  // Always 15, everywhere
console.log(date.getUTCHours()); // Always 14, everywhere

The Trap

If you're in a timezone behind UTC, getDate() might return a different day than getUTCDate()! This is the source of countless "it works on my machine" bugs.

Key Takeaway: Use getUTCDate() and UTC methods for storing/calculating dates; use getDate() and local methods only when displaying to users in their timezone.