@heyhru/common-util-timezone
v0.2.1
Published
User-timezone-aware date utilities based on Intl.DateTimeFormat, usable in server, browser, and React Native
Readme
@heyhru/common-util-timezone
User-timezone-aware date utilities built on Intl.DateTimeFormat. No external dependencies. Works in Node.js, browser, and React Native.
Usage
import {
USER_TZ,
tzParts,
tzStartOfDay,
tzDateKey,
tzWeekday,
tzTimeHHMM,
tzShortDate,
tzDayRange,
} from "@heyhru/common-util-timezone";
// "America/New_York" | "Asia/Shanghai" | ...
console.log(USER_TZ);
// Break a timestamp into local date parts
const parts = tzParts(Date.now());
// { year, month, day, hour, minute, second, weekday }
// 00:00:00 of the day in user's timezone (DST-safe)
const startMs = tzStartOfDay(Date.now());
// "2024.03.15"
const key = tzDateKey(Date.now());
// 0 (Sun) … 6 (Sat)
const wd = tzWeekday(Date.now());
// "9:05"
const hhmm = tzTimeHHMM(Date.now());
// "Mar 15"
const short = tzShortDate(Date.now());
// { startSec, endSec } — unix seconds for the full day
const range = tzDayRange("2024.03.15");API
| Export | Description |
|---|---|
| USER_TZ | User's IANA timezone string |
| tzParts(ms) | Break timestamp into TzParts in user timezone |
| tzStartOfDay(ms) | Unix ms for 00:00:00 of the day (DST-safe) |
| tzDateKey(ms) | Format as "YYYY.MM.DD" |
| tzWeekday(ms) | Weekday number 0(Sun)–6(Sat) |
| tzTimeHHMM(ms) | Format as "H:MM" |
| tzShortDate(ms) | Format as "Mar 5" |
| tzDayRange(dateKey) | { startSec, endSec } for a "YYYY.MM.DD" key |
