Skip to content

Instantly share code, notes, and snippets.

@mvsde
Last active September 2, 2019 09:52
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 mvsde/1be3e668af4b1aba62dc87fe94ef83a2 to your computer and use it in GitHub Desktop.
Save mvsde/1be3e668af4b1aba62dc87fe94ef83a2 to your computer and use it in GitHub Desktop.
`new Date()` that works in Safari
/**
* Parse ISO date string
*
* This convoluted mess is necessary because lovely Safari doesn't use the
* local time zone during `new Date(…)`!
* @param {'YYYY-MM-DD'} date Date string
* @param {'HH:MM:SS'} time Time string
* @returns {Date} Parsed date
*/
export default function (date, time) {
const segements = [
...date.split('-'),
...time.split(':')
].map(item => parseInt(item))
// The second number is the month which is 0-based, so we have to decrement
// value we get from the ISO string.
segements[1] = --segements[1]
return new Date(...segements)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment