Parse a JSON date string
Converts a complete ISO date string in UTC time, the typical format for transmitting
a date in JSON, to a JavaScript Date
instance.
This is a minimal implementation for converting dates retrieved from a JSON API to
a Date
instance which can be used with other functions in the date-fns
library.
The following formats are supported:
2000-03-15T05:20:10.123Z
: The output of .toISOString()
and JSON.stringify(new Date())
2000-03-15T05:20:10Z
: Without milliseconds2000-03-15T05:20:10+00:00
: With a zero offset, the default JSON encoded format in some other languages2000-03-15T05:20:10+05:45
: With a positive or negative offset, the default JSON encoded format in some other languages2000-03-15T05:20:10+0000
: With a zero offset without a colon2000-03-15T05:20:10
: Without a trailing 'Z' symbol2000-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 milliseconds2000-03-15 05:20:10
: With a space instead of a 'T' separator for APIs returning a SQL date without reformattingFor convenience and ease of use these other input types are also supported via [toDate]{@link https://date-fns.org/docs/toDate}:
Date
instance will be clonednumber
will be treated as a timestampAny other input type or invalid date strings will return an Invalid Date
.
// CommonJS var parseJSON = require('date-fns/parseJSON')
// ES 2015 import parseJSON from 'date-fns/parseJSON'
// ESM import { parseJSON } from 'date-fns'
parseJSON(argument)
Name | Description |
---|---|
argument |
A fully formed ISO8601 date string to convert |
Description |
---|
the parsed date in the local time zone |
Type | Description |
---|---|
TypeError |
1 argument required |
© 2021 Sasha Koss and Lesha Koss
Licensed under the MIT License.
https://date-fns.org/v2.29.2/docs/parseJSON