Function parse

  • Parses a string into a Date or number of milliseconds. The string can describe the date in natural language (e.g. "tomorrow", "in 2 hours and 3 minutes", "a month ago", etc.) or be a valid date string (e.g. "2018-01-01").

    Parameters

    • input: string

      The string to parse.

    Returns number | Date

    The parsed date or number of milliseconds.

    Throws

    InvalidInputError This exception is thrown if the input is invalid.

    Example

    Here are some example invocations:

    const ... = parse('1 day ago')
    const ... = parse('three days ago')
    const ... = parse('in 2 hours and 3 minutes')
    const ... = parse('a month')
    const ... = parse('2018-01-01')
    const ... = parse('2018-01-01T00:00:00.000Z')

    Example

    Here are some example invocations that throw an exception:

    parse('in 2 hours and 3 minutes ago')
    parse('a month in the past')