Toolbox-XToolbox-X

Format Date & Time

Format Date & Time — Chronos Documentation.

formatDate

Formats a date into a specified string format using comprehensive formatting tokens.

Signature

formatDate(options?: DateFormatOptions): string

Import

import { formatDate } from "toolbox-x/utils";

Parameter

  • options: Options to control date and time formatting.
interface DateFormatOptions {
    /** Date to format, must be parsable by `Date` constructor. Can be string, number or `Date`. Defaults to current time. */
    date?: string | number | Date;
    /** The desired format (Default format is `'dd, mmm DD, YYYY HH:mm:ss'` = `'Sun, Apr 06, 2025 16:11:55'`). */
    format?: SafeFormat; // 21,000+ predefined formats + any string that uses tokens
    /** Whether to use UTC time. Defaults to `false`. */
    useUTC?: boolean;
}

Return Type

string - Formatted date/time string according to the specified format.

Example

playground.ts

Alias

formatDate can also be accessed via the following aliases:

  • formatDateTime

See Also


Format Tokens

Info

formatDate supports a rich set of format tokens that you can use to customize how a date is displayed. These tokens work across Chronos format methods, and behave similarly to libraries like Moment.js or Day.js.

Use square brackets ([ ]) to escape literal text. Unescaped characters will be treated as formatting tokens and replaced accordingly.

Below is a list of all supported tokens:

TokenOutputExample
or
Full year2025
or
2-digit year25
Full monthJanuary
Short monthJan
2-digit month01-12
Month1-12
2-digit day01-31
Day1-31
Ordinal day1st, 2nd
Full weekdayMonday
Short weekdayMon
Shorter weekdayMo
24-hour (00-23)09
24-hour (0-23)9
12-hour (01-12)02
12-hour (1-12)2
Minutes (00-59)05
Minutes (0-59)5
Seconds (00-59)09
Seconds (0-59)9
Milliseconds (0-999)9
Milliseconds (000-999)009
AM/PMPM
am/pmpm
TZ Offset ±HH:mm+06:00 or Z (UTC)

Caution

  • To output raw text (i.e., not interpreted as a date token), wrap it in square brackets.
  • For example, [Today is] ddd results in Today is Sunday, and YYYY[ year] results in 2025 year.
  • Supported format tokens include: YYYY, YY, MMMM, MMM, MM, M, DD, D, dd, ddd, Do, HH, H, hh, h, mm, m, ss, s, ms, mss, a, A, and ZZ.
  • Any token not wrapped in brackets will be parsed and replaced with its corresponding date component.

formatDateRelative

Formats a date as a relative time string (e.g., "5m ago", "2h from now"). Falls back to a formatted date string for dates older than 7 days.

Import

import { formatDateRelative } from "toolbox-x/utils";

Signature

formatDateRelative(date: Maybe<DateArgs>, format?: SafeFormat): string

Parameters

  • date: The date to format — can be a Date object, a date string, or a timestamp number. If undefined, the current date and time will be used.
  • format: Optional format string for dates older than 7 days. Defaults to 'mmm D, yyyy hh:mm a'. Supports the same tokens as formatDate().

Return Type

string - A relative time string if the date is within the last 7 days, otherwise a formatted date string.

Example

playground.ts

Relative Output Ranges

Time DifferenceOutput
< 1 minute"Just now"
< 60 minutes"Xm ago/from now"
< 24 hours"Xh ago/from now"
< 7 days"Xd ago/from now"
≥ 7 daysFormatted date string using format

Aliases

formatDateRelative can also be accessed via the following aliases:

  • formatRelativeDate
  • formatRelativeTime

Notes

  • If the provided date is invalid, the function returns 'Invalid Date!'.
  • The suffix "ago" or "from now" is determined by whether the date is in the past or future.
  • For dates older than 7 days, the output is formatted using the provided format string or the default 'mmm D, yyyy hh:mm a'.

See Also


formatTimePart

Formats a time-only string into a formatted time string, automatically combining it with today's date for proper parsing.

Import

import { formatTimePart } from "toolbox-x/utils";

Signature

formatTimePart(time: string, format?: TimeOnlyFormat): string

Parameters

  • time: Time string to be formatted. Supported formats include:

    • HH:mm → e.g., '14:50'
    • HH:mm:ss → e.g., '14:50:00'
    • HH:mm:ss.mss → e.g., '14:50:00.800'
    • HH:mm+TimeZoneOffset(HH) → e.g., '14:50+06'
    • HH:mm+TimeZoneOffset(HH:mm) → e.g., '14:50+06:00'
    • HH:mm:ss+TimeZoneOffset(HH) → e.g., '14:50:00+06'
    • HH:mm:ss+TimeZoneOffset(HH:mm) → e.g., '14:50:00+05:30'
    • HH:mm:ss.mss+TimeZoneOffset(HH) → e.g., '14:50:00.800+06'
    • HH:mm:ss.mss+TimeZoneOffset(HH:mm) → e.g., '14:50:00.800+06:30'

    Note: Input defaults to today's date and assumes local timezone if no offset is provided.

  • format: Optional format tokens for time part only (default: 'hh:mm:ss a' = '02:33:36 pm').

Return Type

string - Formatted time string in local (system) time.

Example

playground.ts

Behavior Details

  1. Date Combination: Automatically combines the input time with today's date (using YYYY-MM-DD) to create a complete datetime string for parsing.
  2. Timezone Handling:
    • If a timezone offset is provided (e.g., +06:00), it's preserved in the combined datetime
    • If no offset is provided, local system timezone is assumed
  3. Normalization: The function internally normalizes various time formats to ISO-like format for consistent parsing
  4. Output: Always returns time formatted according to the specified tokens, converted to local system time

Supported Time Format Tokens

The format parameter accepts the same time-related tokens as formatDate():

TokenDescriptionExample
24-hour, zero-padded (00-23)14, 23
24-hour (0-23)14, 23
12-hour, zero-padded (01-12)02, 11
12-hour (1-12)2, 11
Minutes, zero-padded (00-59)05, 30
Minutes (0-59)5, 30
Seconds, zero-padded (00-59)09, 45
Seconds (0-59)9, 45
Milliseconds (0-999)123
Milliseconds, zero-padded (000-999)123
AM/PM uppercaseAM, PM
am/pm lowercaseam, pm

Use Cases

  • Formatting time inputs from forms or APIs that only provide time
  • Displaying time-only data in user-friendly formats
  • Converting 24-hour time to 12-hour format with AM/PM indicators
  • Adjusting times from different timezones to local display

Notes

  • This function is ideal when you only have a time component without a specific date
  • For complete datetime formatting with specific dates, use formatDate() instead
  • The function handles timezone offsets but always outputs in local system time
  • Millisecond tokens (ms, mss) are supported but may not show if the input doesn't include milliseconds
  • If date formatting tokens are passed, it will output the current date

Last updated: Fri, May 22, 2026 07:46:19AM (Coordinated Universal Time)

On this page