Skip to main content

Conversion Methods

toDate()

Signature

toDate(): Date

Return Type

Date - Native Date object

Notes

  • Returns new Date instance

Example

new Chronos('2025-01-15').toDate(); // Date object

toUTC()

Signature

toUTC(): Chronos

Return Type

Chronos - UTC instance

Example

new Chronos('2025-01-15').toUTC(); // UTC-converted instance

toLocal()

Signature

toLocal(): Chronos

Return Type

Chronos - Local time instance

Example

Chronos.utc('2025-01-15').toLocal(); // Local time instance

timeZone()

Note

This method is provided by timeZonePlugin. You must register it using Chronos.use(timeZonePlugin) before calling .timeZone(). Once registered, all Chronos instances will have access to the .timeZone() method.

Signature

timeZone(zone: TimeZone | UTCOffSet): Chronos

Parameters

  • zone: Timezone identifier or offset

Return Type

Chronos - Instance in specified timezone

Example

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

Chronos.use(timeZonePlugin);

new Chronos('2025-01-15').timeZone('EST'); // Eastern Time instance
new Chronos('2025-01-15').timeZone('UTC+08:00'); // 8 hours ahead of UTC/GMT

toObject()

Signature

toObject(): ChronosObject

Return Type

ChronosObject - Date components

interface ChronosObject {
year: number;
month: number;
isoMonth: number;
date: number;
weekDay: number;
isoWeekDay: number;
hour: number;
minute: number;
second: number;
millisecond: number;
timestamp: number;
unix: number;
}

Example

new Chronos('2025-01-15').toObject();
// {year: 2025, month: 0, isoMonth: 1, ...}

toArray()

Signature

toArray(): TupleOf<number, 12>

Return Type

TupleOf<number, 12> - Date component values as array of numbers (12 elements, the values of ChronosObject from toObject method)

Example

new Chronos('2025-01-15').toArray();
// [ 2025, 0, 1, 15, 3, 3, 6, 0, 0, 0, 1736899200000, 1736899200 ]