codebox / moment-precise-range

A moment.js plugin to display human-readable date/time ranges
https://codebox.net/pages/moment-date-range-plugin
MIT License
150 stars 91 forks source link

User moment default language insead of forced English #34

Closed dragma closed 6 years ago

dragma commented 6 years ago

Is there any chance to see this usefull package compatible with moment's locale ?

This is an obviously english-only-package : https://github.com/codebox/moment-precise-range/blob/master/moment-precise-range.js#L6

Atm, i'm using this trick to localise my result, but I find it redundant with the moment's locale

moment.frenchPreciseDiff = (toDate, fromDate) => {
  const STRINGS = {
    years: nb => `${nb} an${nb > 1 && 's'}`,
    months: nb => `${nb} mois`,
    days: nb => `${nb} jour${nb > 1 && 's'}`,
    hours: nb => `${nb} heure${nb > 1 && 's'}`,
    minutes: nb => `${nb} mintue${nb > 1 && 's'}`,
    seconds: nb => `${nb} seconde${nb > 1 && 's'}`,
  };
  const data = moment(fromDate).preciseDiff(toDate, true);
  let string = '';
  if (data.years) {
    string = `${string} ${STRINGS.years(data.years)}`;
  }
  if (data.months) {
    string = `${string} ${STRINGS.months(data.months)}`;
  }
  if (data.days) {
    string = `${string} ${STRINGS.days(data.days)}`;
  }
  if (data.hours) {
    string = `${string} ${STRINGS.hours(data.hours)}`;
  }
  if (data.minutes) {
    string = `${string} ${STRINGS.minutes(data.minutes)}`;
  }
  if (data.seconds) {
    string = `${string} ${STRINGS.seconds(data.seconds)}`;
  }
  return string.trim();
};

[...]

const to = moment().add(15, 'd').toDate();
const duration = moment.frenchPreciseDiff(to);
console.log(duration);
codebox commented 6 years ago

Closing because this is a duplicate of https://github.com/codebox/moment-precise-range/issues/6