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

Date.UTC for Creating Dates in UTC

Use Date.UTC(year, month, ...) to get milliseconds for a UTC timestamp, avoiding local timezone interpretation.

Date.UTC for Creating Dates in UTC

You'll learn how to create timestamps in UTC time without your local timezone interfering.

Why Date.UTC Matters

When you use the Date constructor with year, month, and day arguments, JavaScript interprets those values in your local timezone. This can cause unexpected results when you need to work with universal (UTC) time. The Date.UTC() method solves this by treating all inputs as UTC values and returning milliseconds since the Unix epoch.

How Date.UTC Works

Date.UTC() takes the same arguments as the Date constructor (year, month, day, hours, minutes, seconds, milliseconds), but returns a number — the milliseconds since January 1, 1970 00:00:00 UTC. You can then pass this value to new Date() to create a Date object.

// Local timezone interpretation (confusing!)
const localDate = new Date(2024, 0, 15); // Jan 15, 2024 in YOUR timezone
console.log(localDate.toISOString()); // Might show Jan 14 or 15 depending on timezone

// UTC interpretation (consistent everywhere!)
const utcMillis = Date.UTC(2024, 0, 15); // Jan 15, 2024 in UTC
const utcDate = new Date(utcMillis);
console.log(utcDate.toISOString()); // Always "2024-01-15T00:00:00.000Z"

With Full Time Components

// Create a specific UTC moment: March 10, 2024 at 14:30:45
const timestamp = Date.UTC(2024, 2, 10, 14, 30, 45);
const date = new Date(timestamp);
console.log(date.toISOString()); // "2024-03-10T14:30:45.000Z"

Remember: months are still zero-indexed (0 = January, 2 = March).

Key Takeaway: Use Date.UTC(year, month, ...) to get UTC milliseconds, ensuring your dates are interpreted in universal time rather than your local timezone — essential for server applications and consistent date handling across regions.