Skip to content

Instantly share code, notes, and snippets.

@twalker
Created April 13, 2016 04:45
Show Gist options
  • Save twalker/763a8c8979a8f671cc90aa3133d30bf7 to your computer and use it in GitHub Desktop.
Save twalker/763a8c8979a8f671cc90aa3133d30bf7 to your computer and use it in GitHub Desktop.
cli-intl-example.js
// global.Intl = require('intl');
// global.Intl.__applyLocaleSensitivePrototypes();
// Intl.NumberFormat = IntlPolyfill.NumberFormat;
// Intl.DateTimeFormat = IntlPolyfill.DateTimeFormat;
// IntlPolyfill.__applyLocaleSensitivePrototypes();
var IntlPolyfill = require('intl');
Intl.NumberFormat = IntlPolyfill.NumberFormat;
Intl.DateTimeFormat = IntlPolyfill.DateTimeFormat;
IntlPolyfill.__applyLocaleSensitivePrototypes();
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);
var us = date.toLocaleDateString('en-US', options);
function formatTime(locale) {
var formatter = new Intl.DateTimeFormat(locale, {
hour: 'numeric',
minute: 'numeric'
});
return formatter.format(date);
}
function formatTimeProto(locale) {
return date.toLocaleTimeString(locale, {
hour: 'numeric',
minute: 'numeric'
});
}
var gb = date.toLocaleDateString('en-GB', options);
var us = date.toLocaleDateString('en-US', options);
console.log('ar ' + ar);
console.log('gb ' + gb);
console.log('us ' + us);
console.log('ar ' + formatTimeProto('ar'));
console.log('gb ' + formatTimeProto('en-GB'));
console.log('us ' + formatTimeProto('en-US'));
console.log('ar ' + formatTime('ar'));
console.log('gb ' + formatTime('en-GB'));
console.log('us ' + formatTime('en-US'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment