@ver0/now-ish_temporal
v0.0.0
Published
Temporal API adapter for @ver0/now-ish
Maintainers
Readme
This package connects @ver0/now-ish to the Temporal API. It provides a
ready-to-use parse function that turns expressions like now-7d/d into Temporal.ZonedDateTime objects with
nanosecond precision.
If you prefer the modern Temporal API over date-fns, this is the adapter for you.
Installation
npm install @ver0/now-ish_temporal @ver0/now-ish
@ver0/now-ishis a peer dependency.
This package uses @js-temporal/polyfill until Temporal becomes available natively in JavaScript engines.
How to use
The default parser uses UTC timezone and recognizes the now keyword.
import {parse} from '@ver0/now-ish_temporal';
const sevenDaysAgo = parse('now-7d');
const startOfToday = parse('now/d');
const startOfLastWeek = parse('now-1w/w');
// Round to end of period instead of start
const endOfMonth = parse('now/mo', 'round-up');
// Override timezone per call
const tokyoMidnight = parse('now/d', 'round-down', {timezone: 'Asia/Tokyo'});For the full expression syntax, see the core package documentation.
Supported units
ms (millisecond), s (second), m (minute), h (hour), d (day), w (week), mo (month), y (year)
Weeks follow ISO 8601 — they start on Monday.
Custom configuration
You can create a custom parser with a different default timezone, localized keywords, or additional units.
Adding now aliases:
import {createParser} from '@ver0/now-ish';
import {now, units} from '@ver0/now-ish_temporal';
const parse = createParser({
now,
units,
nowAliases: ['now', 'jetzt', 'сейчас'],
timezone: 'Europe/Berlin',
});
parse('jetzt-7d'); // worksAdding unit aliases:
import {createParser} from '@ver0/now-ish';
import {now, units} from '@ver0/now-ish_temporal';
// Add aliases for existing units
const extendedUnits = new Map(units);
extendedUnits.set('t', units.get('d')!); // German: Tag = day
extendedUnits.set('wo', units.get('w')!); // German: Woche = week
const parse = createParser({
now,
units: extendedUnits,
nowAliases: ['now', 'jetzt'],
timezone: 'UTC',
});
parse('jetzt-7t'); // 7 days agoWhy Temporal?
The TC39 Temporal proposal addresses long-standing issues with JavaScript's
Date object: immutability, nanosecond precision, first-class timezone support, and unambiguous DST handling.
While Temporal is not yet natively available, the polyfill provides full functionality today.
Related
- @ver0/now-ish — Core parser, expression syntax, extensibility
- @ver0/now-ish_date-fns — date-fns adapter
