Skip to content

Instantly share code, notes, and snippets.

@rxaviers
Last active August 29, 2015 14:20
Show Gist options
  • Save rxaviers/1697a0b3bd668669e6e4 to your computer and use it in GitHub Desktop.
Save rxaviers/1697a0b3bd668669e6e4 to your computer and use it in GitHub Desktop.

Run:

npm install globalize cldr-data
node myFormatter.js
node myFormatter.js es
node myFormatter.js fr
node myFormatter.js it
node myFormatter.js de
node myFormatter.js ar
node myFormatter.js zh

Output:

date = Thu Apr 30 2015 10:40:42 GMT-0300 (BRT)
> last Thursday at 10:40 AM

date = Thu Apr 30 2015 10:40:42 GMT-0300 (BRT)
> el jueves pasado, 10:40 a. m.

date = Thu Apr 30 2015 10:40:42 GMT-0300 (BRT)
> jeudi dernier 10:40 AM

date = Thu Apr 30 2015 10:40:42 GMT-0300 (BRT)
> giovedì scorso 10:40 AM

date = Thu Apr 30 2015 10:40:43 GMT-0300 (BRT)
> letzten Donnerstag um 10:40 vorm.

date = Thu Apr 30 2015 10:40:43 GMT-0300 (BRT)
> الخميس الماضي ١٠،٤٠ ص

date = Thu Apr 30 2015 10:40:43 GMT-0300 (BRT)
> 上周四 上午10:40
var Globalize = require("globalize");
var locale = process.argv.length === 3 ? process.argv[2] : "en";
Globalize.load(require("cldr-data").entireSupplemental());
Globalize.load(require("cldr-data").entireMainFor(locale));
Globalize.locale(locale);
// Globalize.loadMessages({
// en: {
// lastWeekdayFormat: {
// pastWeek: "{1} at {0}"
// }
// }
// });
var automaticallyGeneratedMsg = {};
automaticallyGeneratedMsg[locale] = {
lastWeekdayFormat: {
pastWeek: Globalize.cldr.main("dates/calendars/gregorian/dateTimeFormats/full").replace(/'/g, "")
}
};
Globalize.loadMessages(automaticallyGeneratedMsg);
var weekdayFormatter = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"].map(function(weekday) {
return Globalize.relativeTimeFormatter(weekday);
});
var msgFormatter = Globalize.messageFormatter("lastWeekdayFormat/pastWeek");
var timeFormatter = Globalize.dateFormatter({skeleton: "hm"});
function lastWeekdayFormatter(date) {
return msgFormatter(
timeFormatter(date),
weekdayFormatter[date.getDay()](-1)
);
}
var date = new Date();
date.setDate(date.getDate() - 7);
console.log("date =", date);
console.log(">", lastWeekdayFormatter(date));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment