Function coerceToDate

  • Parses various date formats into a JavaScript Date object

    This function handles different date input formats commonly found in conversation data:

    • 10-digit timestamps (Unix seconds) - automatically converted to milliseconds
    • 13-digit timestamps (Unix milliseconds) - used directly
    • String representations of timestamps
    • ISO date strings (e.g., "2025-06-01T12:30:00Z")
    • Simple date strings (e.g., "2025-06-01") - time defaults to 00:00:00
    • Date strings with space-separated time (e.g., "2025-06-01 12:30:00")

    Note: This function follows JavaScript Date constructor behavior for date parsing. Some invalid dates like "2025-02-30" auto-correct to valid dates (becomes "2025-03-02"), while malformed strings like "2025-13-01" or "2025-06-01T25:00:00" return null.

    Example

    coerceToDate('2025-06-01') // Returns Date object set to 2025-06-01 00:00:00
    coerceToDate('2025-06-01T12:30:00Z') // Returns Date object with specified time
    coerceToDate(1748834578) // Returns Date object (10-digit timestamp in seconds)
    coerceToDate(1748834578000) // Returns Date object (13-digit timestamp in milliseconds)
    coerceToDate('1748834578') // Returns Date object (string timestamp converted)
    coerceToDate(null) // Returns null
    coerceToDate('invalid-date') // Returns null

    Parameters

    • dateInput: undefined | null | string | number

    Returns null | Date

Generated using TypeDoc