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
Datebehavior
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
- Similar to
toISOString()but preserves time zone offset
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
/** `BCP47` locale string or `Intl.Locale` object that contain one or more language or locale tags */
type $LocalArguments = LooseLiteral<LocaleCode | Split<LocaleCode, '-'>[0]> | Intl.Locale;
/** `BCP47` locale string, array of locale strings, `Intl.Locale` object, or array of `Intl.Locale` objects that contain one or more language or locale tags. */
type LocalesArguments = $LocalArguments | $LocalArguments[];
/** Locale calendars supported by `Intl` API */
type LocaleCalendar = 'buddhist' |'chinese' | 'coptic' | 'ethiopic' | 'gregory' | 'hebrew' | 'indian' | 'islamic' | ... ;
/** Locale numbering systems supported by `Intl` API */
type NumberingSystem = 'adlm' | 'ahom' | 'arab' | 'arabext' | 'bali' | 'beng' | 'bhks' | 'brah' | 'cakm' | 'cham' | ... ;
/** Extends `Intl.DateTimeFormatOptions` with improved type system. */
interface DateTimeFormatOptions extends Intl.DateTimeFormatOptions {
/** Time zone identifier to use. */
timeZone?: $TimeZoneIdentifier;
/** Locale calendar system to use. */
calendar?: LocaleCalendar;
/** Locale numbering system to use. */
numberingSystem?: NumberingSystem;
}
Return Type
string - Localized date string
Notes
- Wrapper around native
Date.toLocaleString()
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.jsutil.inspect - Includes
Chronosprefix
Example
new Chronos('2025-01-15').inspect();
// "[Chronos 2025-01-15T00:00:00.000-05:00]"