Skip to content

Date Utility

A pre-configured dayjs re-export, plus small standalone helpers for sleeping, weekday math, timezone conversion, and high-resolution timing.

In one example

typescript
import { dayjs, sleep, getDateTz } from '@venizia/ignis-helpers';

const now = dayjs().format('YYYY-MM-DD HH:mm:ss');

await sleep(2000); // pause for 2 seconds

const tokyoTime = getDateTz({ date: '2023-10-27T10:00:00Z', timezone: 'Asia/Tokyo' });

Functions

FunctionSignatureWhat it does
dayjsre-exported dayjs objectThe dayjs factory, pre-loaded with plugins - use it exactly like raw dayjs.
sleepsleep(ms: number): Promise<void>Resolves after ms milliseconds (setTimeout wrapped in a Promise).
isWeekdayisWeekday(date: string | dayjs.Dayjs): booleantrue when date falls Monday through Friday (ISO weekday 1-5).
getPreviousWeekdaygetPreviousWeekday(opts?: { date?: string | dayjs.Dayjs }): dayjs.DayjsWalks backward a day at a time from date (default: today) until it lands on a weekday.
getNextWeekdaygetNextWeekday(opts?: { date?: string | dayjs.Dayjs }): dayjs.DayjsWalks forward a day at a time from date (default: today) until it lands on a weekday.
getDateTzgetDateTz(opts: { date: string; timezone: string; useClientTz?: boolean; timeOffset?: number }): dayjs.DayjsParses date and converts it to timezone, optionally shifting by timeOffset hours.
hrTimehrTime(): numberHigh-resolution seconds from process.hrtime(), rounded to 9 decimal places - for benchmarking.

Notes

  • Plugins loaded once at module import: CustomParseFormat, UTC, Timezone, Weekday, IsoWeek.
  • Default timezone is Asia/Ho_Chi_Minh, set via dayjs.tz.setDefault() at module load. Override with the APP_ENV_APPLICATION_TIMEZONE environment variable - it is read once, so changing it at runtime needs a restart.
  • getPreviousWeekday / getNextWeekday are day-by-day loops, not calendar lookups. They call isWeekday() after each step, so the worst case (stepping over a weekend) is only 2-3 iterations.
  • getDateTz's useClientTz defaults to false and controls whether dayjs().tz() keeps the original wall-clock time or converts it - see the Day.js Timezone plugin docs for the exact semantics.

See also

Files: