Skip to content

Instantly share code, notes, and snippets.

@twalker
Last active April 13, 2016 03:08
Show Gist options
  • Save twalker/d33941123b89fe28c059931679994673 to your computer and use it in GitHub Desktop.
Save twalker/d33941123b89fe28c059931679994673 to your computer and use it in GitHub Desktop.
Repro example for needing to Polyfill Date/Number prototypes in node.
var express = require('express');
var app = express();
if (global.Intl) {
// `Intl` exists, but it doesn't have the data we need, so load the
// polyfill and patch the constructors we need with the polyfill's.
var IntlPolyfill = require('intl');
Intl.NumberFormat = IntlPolyfill.NumberFormat;
Intl.DateTimeFormat = IntlPolyfill.DateTimeFormat;
// HERE, Date/Number prototypes still seem to need polyfilled (node@4.2.1, node@5.8.0).
IntlPolyfill.__applyLocaleSensitivePrototypes();
} else {
// No `Intl`, so use and load the polyfill.
global.Intl = require('intl');
// HERE, Even when replacing Intl with the polyfill,
// the the prototypes need polyfilled.
global.Intl.__applyLocaleSensitivePrototypes();
}
app.get('/', function (req, res) {
var date = new Date();
var options = {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric'
};
var ar = date.toLocaleDateString('ar', options);
var gb = date.toLocaleDateString('en-GB', options);
res.send('ar: ' + ar + ' gb: ' + gb);
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment