Static Methods
use()
Injects a plugin into the Chronos system. This enables dynamic extension of the Chronos
class at runtime by registering external functionality as methods on its prototype.
One-time Injection
A plugin is only injected once per runtime. Re-registering the same plugin has no effect.
Signature
static use(plugin: ChronosPlugin): void
Parameters
Name | Type | Description |
---|---|---|
plugin | ChronosPlugin | A plugin function that receives the Chronos class constructor and augments it. |
Example
import { Chronos } from 'nhb-toolbox';
import { timeZonePlugin } from 'nhb-toolbox/plugins/timeZonePlugin';
Chronos.use(timeZonePlugin); // Injects timeZone() method
Once injected, the plugin methods become available on all Chronos
instances:
const c = Chronos.now();
c.timeZone('UTC+06:00');
Notes
info
- Plugins should be injected before any instance creation.
- Internally, Chronos maintains a
#plugins
set to prevent duplicate injections. - This system is ideal for modular features like
seasons
,zodiac
, ortimeZone
etc. support.
parse()
Signature
static parse(dateStr: string, format: string): Chronos
Parameters
dateStr
: Date string to parseformat
: Format string
Return Type
Chronos
- Parsed date
Supported Tokens
YYYY
,YY
- YearMM
,M
- MonthDD
,D
- DayHH
,H
- Hourmm
,m
- Minutess
,s
- Second
Example
Chronos.parse('15-01-2025', 'DD-MM-YYYY'); // Jan 15 2025