Last active
September 2, 2019 09:52
-
-
Save mvsde/1be3e668af4b1aba62dc87fe94ef83a2 to your computer and use it in GitHub Desktop.
`new Date()` that works in Safari
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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