Course contentsShow
← Web Development
Lesson 2749 of 2,98249. Dates, Times, and Numbers on the WebFree lesson

Storing Times: Always Use UTC

Learn best practices for storing timestamps in UTC and converting to local time only for display.

Storing Times: Always Use UTC

You'll learn why storing all timestamps in UTC prevents data corruption and makes timezone conversion trivial.

The Core Principle

When you save a timestamp to a database or send it to an API, always store it in UTC (Coordinated Universal Time). Never store local times with timezone information mixed in, and never store times adjusted to a specific timezone like "America/New_York" or "Europe/London".

Think of UTC as the universal language of time. Just as you might store all prices in a base currency (USD) and convert to local currencies only for display, you store all times in UTC and convert to local timezones only when showing them to users.

Why This Matters

Imagine storing user activity timestamps in their local timezones. A user in New York creates a post at "2 PM EST", another in Tokyo replies at "3 AM JST". Without careful handling, you can't reliably determine which happened first. Daylight Saving Time changes make this worse—what does "2:30 AM" mean on the day clocks spring forward?

By storing everything in UTC, you have a single, unambiguous timeline. Comparison, sorting, and arithmetic operations work correctly without timezone math. You only convert to local time at the display layer—in the browser, using the user's detected timezone.

In Practice

// ❌ BAD: Storing local time
const timestamp = new Date(); // Browser's local time
await saveToDatabase({ created_at: timestamp });

// ✅ GOOD: Store as UTC ISO string
const timestamp = new Date().toISOString(); // Always UTC
await saveToDatabase({ created_at: timestamp }); // "2025-05-15T14:23:45.000Z"

When retrieving, display with libraries like Luxon or date-fns that handle timezone conversion automatically.

Key Takeaway: Store all timestamps in UTC format and convert to local timezones only at the display layer—this prevents timezone bugs, DST issues, and makes time comparison reliable across your entire system.