Service for formatting date and time values.

Hierarchy

  • DateTimeFormatter

Methods

Methods

  • Format the given date as the given type.

    Note
    If a string is supplied to the date parameter, it will be converted to a Date object before formatting. See parse for details.

    Returns

    A string representation of the date.

    Parameters

    • date: string | Date

      The date to format.

    • options: DateTimeType | DateTimeFormatterOptions

      Either a string specifying the type to format the date as, or a DateTimeFormatterOptions object.

    Returns string

  • Parse a date value, converting it to local time.

    A date value passed as a string may be converted as a date-only value if type is 'date', 'year', 'quarter', 'month' or 'week'. This basically means returning dates as they are stored in the database, by compensating for any time difference.

    If the date value includes time but not time zone offset, it is always interpreted as local time.

    A date value passed as a Date is returned unmodified.

    Example

    // Converting a saved date value to 2023-10-24 in local time
    // The time component of the returned date object should be ignored.
    const parsedDate = dateTimeFormatter.parse('2023-10-24T00:00:00.000Z', 'date');

    Example

    // Parsing a date value as local time
    const parsedDate = dateTimeFormatter.parse('2023-10-24 23:59:59', 'date');

    Example

    // Parsing a date-time value including time zone offset
    const parsedDate = dateTimeFormatter.parse('2023-10-24T13:00:00+02:00', 'time');

    Parameters

    • date: string | Date

      The date to parse.

    • type: DateTimeType

      A string specifying the type to parse the date as.

    Returns Date