Skip to content

Instantly share code, notes, and snippets.

@suissa
Created December 9, 2019 03:46
Show Gist options
  • Save suissa/f81696bd872dc654c571e88879722e54 to your computer and use it in GitHub Desktop.
Save suissa/f81696bd872dc654c571e88879722e54 to your computer and use it in GitHub Desktop.
const toLower = (str) => str.toLowerCase()
const isEqual = (a, b) => a === b
const AND = (args) => args.reduce((r, t) => r && t , true)
const getTSFromISODate = (date) => new Date(date).getTime()
const getISODateFromTS = (date) => new Date(date)
const isTSFromISODate = (from, to) =>
AND([
isEqual(toLower(from), 'iso'),
isEqual(toLower(to), 'timestamp')
])
const convertDate = (date, {from = 'iso', to = 'timestamp'}) =>
(isTSFromISODate(from, to))
? getTSFromISODate(date)
: getISODateFromTS(date)
console.log(
'iso to ts',
new Date().toISOString(),
convertDate(
new Date().toISOString(),
{from: 'iso', to: 'timestamp'}
)
)
console.log(
'ts to iso',
Date.now(),
convertDate(
Date.now(),
{from: 'timestamp', to: 'iso'}
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment