Skip to content

Instantly share code, notes, and snippets.

@kawanet
Created December 1, 2017 05:00
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 kawanet/4947237d84ba6efb16183ffcc23e982a to your computer and use it in GitHub Desktop.
Save kawanet/4947237d84ba6efb16183ffcc23e982a to your computer and use it in GitHub Desktop.
Safari has a bug on parsing Date when both its time and timezone are negative value.
/**
* Safari has a bug on parsing Date when both its time and timezone are negative value.
*
* @author @kawanet
* @license MIT
* @see https://github.com/kawanet/timestamp-nano/blob/master/test/52.timezone.js
* @see https://gist.github.com/kawanet/4947237d84ba6efb16183ffcc23e982a
*/
function checkDateBug() {
var date = +new Date("1969-12-31T23:59:59.999-01:00");
return date % 1000 !== 999;
}
if ("undefined" !== typeof module) module.exports = checkDateBug;
@kawanet
Copy link
Author

kawanet commented Dec 1, 2017

Safari looks to have a JavaScript bug to give 1 millisecond difference on parsing Date string before 1970 on the Western Hemisphere.

This gist above could detect the environment which has the bug as below:

new Date("1969-12-31T23:59:59.999+01:00").toJSON();
// OK! => "1969-12-31T22:59:59.999Z"

new Date("1969-12-31T23:59:59.999-01:00").toJSON();
// NG! => "1970-01-01T00:59:59.998Z"

The number of (milli)seconds must be 59.999 but NOT 59.998. It gives one millisecond inaccuracy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment