Dayjs Timezone-Safe Date Conversion

Convert timestamps between timezones with Dayjs and its UTC plugin, keeping wall-clock semantics intact and formatting per-locale output.

JavascriptData Integrity

Code

javascriptsnippet
22 lines
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import timezone from 'dayjs/plugin/timezone';

dayjs.extend(utc);
dayjs.extend(timezone);

const raw = '2026-10-22T09:30:00Z';

const tokyo = dayjs(raw).tz('Asia/Tokyo');
const berlin = dayjs(raw).tz('Europe/Berlin');
const ny = dayjs(raw).tz('America/New_York');

console.log('tokyo wall clock:', tokyo.format('YYYY-MM-DD HH:mm'));
console.log('berlin wall clock:', berlin.format('YYYY-MM-DD HH:mm'));

const localMeeting = dayjs.tz('2026-10-22 15:00', 'Asia/Shanghai');
const utcInstant = localMeeting.utc();
console.log('utc instant:', utcInstant.toISOString());

const deadline = dayjs.tz('2026-03-08 02:30', 'America/New_York');
console.log('ambiguous local time normalized to:', deadline.format());

How it works

All the work happens on the top-level bindings: raw, tokyo, berlin, ny.

What to watch for

  • Plan for an install step: dayjs, dayjs/plugin/utc and dayjs/plugin/timezone are a real dependency, resolved by the package manager.

Putting it to use

The demo output is labeled (tokyo wall clock:, berlin wall clock:, utc instant:), so the output doubles as a step-by-step trace of what the file did.

Core lines

const localMeeting = dayjs.tz('2026-10-22 15:00', 'Asia/Shanghai');
const deadline = dayjs.tz('2026-03-08 02:30', 'America/New_York');
const berlin = dayjs(raw).tz('Europe/Berlin');

Interactive Tools for This Topic

FAQ

Which packages must be installed first?
Yes. The imports resolve to `dayjs`, `dayjs/plugin/utc` and `dayjs/plugin/timezone`, which the package manager must provide.