vitest

timezones

dayjs

import dayjs from "dayjs";
import tz from "dayjs/plugin/timezone";
dayjs.extend(tz);

const now = dayjs();
const sydneyDate = dayjs("2025-03-03").tz("Australia/Sydney");
const isToday = now.isSame(sydneyDate, "day");

Intl.DateTimeFormat

const date = new Date("2025-03-03");
const asSydneyTime = date.toLocaleDateString("en-AU", {
  timeZone: "Australia/Sydney",
});

vitest

Running tests in multiple time zones

// vitest.config.ts
import { defineConfig } from "vitest/config";

export default defineConfig({
  test: {
    globals: true,
    workspace: [
      {
        extends: true,
        test: {
          name: "GMT",
          env: {
            TZ: "UTC",
          },
        },
      },
      {
        extends: true,
        test: {
          name: "GMT+13",
          env: {
            TZ: "America/Auckland",
          },
        },
      },
    ],
  },
});

Mocking now & timezones in tests

export const mockNow = (now: string, timeZone?: string) => {
  beforeEach(() => {
    vi.useFakeTimers();
    const date = new Date(now);
    vi.setSystemTime(date);
    if (timeZone) {
      vi.spyOn(globalThis.Intl, "DateTimeFormat").mockImplementation(() => ({
        resolvedOptions: () => ({
          timeZone, // Your timezone, rest is irrelevant
          calendar: "gregory",
          locale: "en-AU",
          numberingSystem: "latin",
        }),
        format: () => "1/1/2022",
        formatRange: () => "1/1/2022 – 1/1/2023",
        formatRangeToParts: () => [],
        formatToParts: () => [],
      }));
    }
  });
  afterEach(() => {
    vi.useRealTimers();
  });
};

chrome

Simulate different time zones in Chrome DevTools:

  1. Open DevTools
  2. Open Console Drawer (Esc)
  3. Click on the three dots in the left corner
  4. Select Sensors
  5. Enter a Timezone ID like UTC or Australia/Sydney