Skip to main content

Chronos Wrapper Function

Overview

info

Function wrapper around the Chronos class that provides identical functionality with a simpler interface. All Chronos features are available through this function.

Key Features

  • Same functionality as new Chronos() but with function syntax
  • Full access to all Chronos instance methods
  • Inherits all static methods from Chronos class (chronos without call signature)
  • Supports all Chronos input types
  • Maintains identical type safety

🧩 Plugin System

Chronos supports a modular plugin system that allows you to extend its capabilities without bloating the core. Plugin methods are not available by default, you must explicitly register them using the .use() or .register() static method.

How it works

import { chronos } from 'nhb-toolbox';
import { seasonPlugin } from 'nhb-toolbox/plugins/seasonPlugin';

// Register the plugin before using its methods
chronos.use(seasonPlugin);
// or
chronos.register(seasonPlugin);

console.log(chronos().season()); // ✅ Safe to use after plugin registration
info

Each plugin enhances the Chronos prototype and becomes available globally after registration.

Usage

Import

import { chronos } from 'nhb-toolbox';
// or
import { chronos } from 'nhb-toolbox/chronos';

Basic Instantiation

// Function style
const date = chronos('2023-12-31')

date.year // 2023
date.formatStrict() // Formatted date string

// Class style (equivalent)
const date = new Chronos('2023-12-31')

date.year // 2023
date.formatStrict() // Formatted date string

With Components

const date = chronos(2023, 12, 31, 15, 30)

date.year // 2023
date.formatStrict() // Formatted date string

Available Methods & Properties

The wrapper provides access to all Chronos methods through the returned instance:

Static Methods

All Chronos static methods are available directly on the wrapper function:

List of Static Methods
MethodShort Description
now()Returns the number of milliseconds since 1970-01-01 UTC.
use()Injects a plugin into the Chronos system.
register()Alias for use()
utc()Creates a UTC-based Chronos instance.
with()Creates a new instance with the provided time component(s).
parse()Parses a date string with a given format (limited support only).
max()Returns latest Chronos.
min()Returns earliest Chronos.
today()Returns the current date in a specified format in local time.
tomorrow()Returns a new Chronos instance representing tomorrow's date.
yesterday()Returns a new Chronos instance representing yesterday's date.
formatTimePart()Formats a time-only string into a formatted time string.
getDatesForDay()Returns ISO date strings for each occurrence of a weekday.
isLeapYear()Checks if the year in the date or year is a leap year.
isValidChronos()Checks if the given value is an instance of Chronos.
isValidDate()Checks if the given value is a valid Date object.
isDateString()Checks if the given value is a valid date string.

Some Examples

import { Chronos } from 'nhb-toolbox';
import { timeZonePlugin } from 'nhb-toolbox/plugins/timeZonePlugin';

// Using wrapper function
chronos.parse('2023-12-31', 'YYYY-MM-DD')
chronos.use(timeZonePlugin)
chronos.today()
chronos.isLeapYear(2024)

// Using class (equivalent)
Chronos.parse('2023-12-31', 'YYYY-MM-DD')
Chronos.today()
Chronos.isLeapYear(2024)

Aliases

The chronos function is also available under the following aliases:

  • chronosjs
  • chronosts
  • chronus
  • chronusjs
  • chronusts

These aliases exist to support flexible naming conventions and common user preferences.

Full Documentation

For complete method documentation, see the Chronos class reference. All methods shown there are available through this wrapper.