Skip to main content

String Methods


toISOString()

Returns a date as a string value in ISO format (UTC).

Signature

toISOString(): string

Return Type

string - Standard ISO 8601 string

Notes

  • Always returns UTC time
  • Consistent with native Date behavior

Example

new Chronos('2025-01-01T00:00:00-05:00').toISOString();
// "2025-01-01T05:00:00.000Z"

toLocalISOString()

Returns ISO time string in appropriate time zone with offset.

Signature

toLocalISOString(): string

Return Type

string - ISO string with time zone offset

Notes

Example

new Chronos('2025-01-01T00:00:00-05:00').toLocalISOString();
// "2025-01-01T00:00:00.000-05:00"

toLocaleString()

Wrapper over native Date object's toLocaleString method with improved type system.

Signature

toLocaleString(locales?: LocalesArguments, options?: DateTimeFormatOptions): string

Parameters

  • locale: Locale string(s)
  • options: Enhanced Intl.DateTimeFormat options

Type Definitions

/** Locale arguments for `toLocaleString` method. Includes `BCP47` tags and `Intl.Locale`. */
type LocalesArguments = LocaleCode | Intl.Locale | Array<LocaleCode | Intl.Locale>;

/** Format options for `toLocaleString` method. Extends `Intl.DateTimeFormatOptions `to update `timeZone` option. */
interface DateTimeFormatOptions extends Intl.DateTimeFormatOptions {
timeZone?: $TimeZoneIdentifier;
}

Return Type

string - Localized date string

Notes

Example

new Chronos('2025-01-15').toLocaleString('en-US', { 
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric'
});
// "Wednesday, January 15, 2025"

toString()

Returns a string representation of a date.

Signature

toString(): string

Return Type

string - Date string

Notes

  • Returns localized string representation.
  • Includes timezone information when relevant.

Example

new Chronos('2025-01-15').toString();
// "Sun Jan 15 2025 00:00:00 GMT-0500 (Eastern Standard Time)"

toJSON()

Enables JSON.stringify to show readable output. Calls toLocalISOString method.

Signature

toJSON(): string

Return Type

string - ISO string

Notes

  • Used by JSON.stringify()
  • Same as toLocalISOString()

Example

JSON.stringify({ date: new Chronos('2025-01-15') });
// '{"date":"2025-01-15T06:00:00.000+06:00"}'

inspect()

Returns a debug-friendly string for console.log or util.inspect.

Signature

inspect(): string

Return Type

string - Debug-friendly string

Notes

  • Used by Node.js util.inspect
  • Includes Chronos prefix

Example

new Chronos('2025-01-15').inspect();
// "[Chronos 2025-01-15T00:00:00.000-05:00]"