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.
Code
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/utcanddayjs/plugin/timezoneare 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
Timezone Offset FinderLook up the UTC offset, abbreviation, and local time for any IANA time zone at a given instant, and see whether the zone observes daylight saving.A/B Test Sample Size CalculatorCalculate required per-variant sample size and experiment duration for two-proportion A/B tests from baseline conversion, MDE, alpha, and statistical power.CSS Unit ConverterConvert a length between px, rem, em, pt, pc, in, cm, mm, and percent using a configurable root font size, following the 96dpi CSS definition.Date Add/Subtract CalculatorAdd or subtract days, weeks, months, or years from a date, clamping month ends such as January 31 plus one month.
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.
Report a problem with this exampleOpens a pre-filled issue on GitHub.