Skip to content

Instantly share code, notes, and snippets.

@skellock
Created February 28, 2017 00:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skellock/74d5d40c8cf9495fd7318514af31f5bc to your computer and use it in GitHub Desktop.
Save skellock/74d5d40c8cf9495fd7318514af31f5bc to your computer and use it in GitHub Desktop.
// Source: https://github.com/szwacz/fs-jetpack/blob/master/lib/read.js#L22-L32
const fromJsonDate = value => {
const reISO = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*))(?:Z|(\+|-)([\d|:]*))?$/
if (typeof value === 'string') {
if (reISO.exec(value)) {
return new Date(value)
}
}
return value
}
const someDate = new Date()
const stringified = someDate.toJSON()
const dateified = fromJsonDate(stringified)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment