UNPKG

json-date-parser

Version:

Minimal function suitable for passing to JSON.parse to revive common date-time strings into Date objects

22 lines 684 B
"use strict"; var reISO = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.{0,1}\d*))(?:Z|(\+|-)([\d|:]*))?$/; var reMsAjax = /^\/Date\((d|-|.*)\)[\/|\\]$/; function jsonDateParser(_, value) { var parsedValue = value; if (typeof value === 'string') { var a = reISO.exec(value); if (a) { parsedValue = new Date(value); } else { a = reMsAjax.exec(value); if (a) { var b = a[1].split(/[-+,.]/); parsedValue = new Date(b[0] ? +b[0] : 0 - +b[1]); } } } return parsedValue; } exports.jsonDateParser = jsonDateParser; //# sourceMappingURL=index.js.map