parseJSON.d.cts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import type { ContextOptions } from "./types.js";
  2. /**
  3. * The {@link parseJSON} function options.
  4. */
  5. export interface ParseJSONOptions<DateType extends Date = Date>
  6. extends ContextOptions<DateType> {}
  7. /**
  8. * Converts a complete ISO date string in UTC time, the typical format for transmitting
  9. * a date in JSON, to a JavaScript `Date` instance.
  10. *
  11. * This is a minimal implementation for converting dates retrieved from a JSON API to
  12. * a `Date` instance which can be used with other functions in the `date-fns` library.
  13. * The following formats are supported:
  14. *
  15. * - `2000-03-15T05:20:10.123Z`: The output of `.toISOString()` and `JSON.stringify(new Date())`
  16. * - `2000-03-15T05:20:10Z`: Without milliseconds
  17. * - `2000-03-15T05:20:10+00:00`: With a zero offset, the default JSON encoded format in some other languages
  18. * - `2000-03-15T05:20:10+05:45`: With a positive or negative offset, the default JSON encoded format in some other languages
  19. * - `2000-03-15T05:20:10+0000`: With a zero offset without a colon
  20. * - `2000-03-15T05:20:10`: Without a trailing 'Z' symbol
  21. * - `2000-03-15T05:20:10.1234567`: Up to 7 digits in milliseconds field. Only first 3 are taken into account since JS does not allow fractional milliseconds
  22. * - `2000-03-15 05:20:10`: With a space instead of a 'T' separator for APIs returning a SQL date without reformatting
  23. *
  24. * For convenience and ease of use these other input types are also supported
  25. * via [toDate](https://date-fns.org/docs/toDate):
  26. *
  27. * - A `Date` instance will be cloned
  28. * - A `number` will be treated as a timestamp
  29. *
  30. * Any other input type or invalid date strings will return an `Invalid Date`.
  31. *
  32. * @typeParam ResultDate - The result `Date` type, it is the type returned from the context function if it is passed, or inferred from the arguments.
  33. *
  34. * @param dateStr - A fully formed ISO8601 date string to convert
  35. * @param options - An object with options
  36. *
  37. * @returns The parsed date in the local time zone
  38. */
  39. export declare function parseJSON<ResultDate extends Date = Date>(
  40. dateStr: string,
  41. options?: ParseJSONOptions<ResultDate> | undefined,
  42. ): ResultDate;