Application Android et IOS pour l'amicale des élèves
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DateManager.js 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // @flow
  2. import i18n from 'i18n-js';
  3. /**
  4. * Singleton used to manage date translations.
  5. * Translations are hardcoded as toLocaleDateString does not work on current android JS engine
  6. */
  7. export default class DateManager {
  8. static instance: DateManager | null = null;
  9. daysOfWeek = [];
  10. monthsOfYear = [];
  11. constructor() {
  12. this.daysOfWeek.push(i18n.t("date.daysOfWeek.sunday")); // 0 represents sunday
  13. this.daysOfWeek.push(i18n.t("date.daysOfWeek.monday"));
  14. this.daysOfWeek.push(i18n.t("date.daysOfWeek.tuesday"));
  15. this.daysOfWeek.push(i18n.t("date.daysOfWeek.wednesday"));
  16. this.daysOfWeek.push(i18n.t("date.daysOfWeek.thursday"));
  17. this.daysOfWeek.push(i18n.t("date.daysOfWeek.friday"));
  18. this.daysOfWeek.push(i18n.t("date.daysOfWeek.saturday"));
  19. this.monthsOfYear.push(i18n.t("date.monthsOfYear.january"));
  20. this.monthsOfYear.push(i18n.t("date.monthsOfYear.february"));
  21. this.monthsOfYear.push(i18n.t("date.monthsOfYear.march"));
  22. this.monthsOfYear.push(i18n.t("date.monthsOfYear.april"));
  23. this.monthsOfYear.push(i18n.t("date.monthsOfYear.may"));
  24. this.monthsOfYear.push(i18n.t("date.monthsOfYear.june"));
  25. this.monthsOfYear.push(i18n.t("date.monthsOfYear.july"));
  26. this.monthsOfYear.push(i18n.t("date.monthsOfYear.august"));
  27. this.monthsOfYear.push(i18n.t("date.monthsOfYear.september"));
  28. this.monthsOfYear.push(i18n.t("date.monthsOfYear.october"));
  29. this.monthsOfYear.push(i18n.t("date.monthsOfYear.november"));
  30. this.monthsOfYear.push(i18n.t("date.monthsOfYear.december"));
  31. }
  32. /**
  33. * Get this class instance or create one if none is found
  34. * @returns {DateManager}
  35. */
  36. static getInstance(): DateManager {
  37. return DateManager.instance === null ?
  38. DateManager.instance = new DateManager() :
  39. DateManager.instance;
  40. }
  41. /**
  42. * Gets a translated string representing the given date.
  43. *
  44. * @param dateString The date with the format YYYY-MM-DD
  45. * @return {string} The translated string
  46. */
  47. getTranslatedDate(dateString: string) {
  48. let dateArray = dateString.split('-');
  49. let date = new Date();
  50. date.setFullYear(parseInt(dateArray[0]), parseInt(dateArray[1]) - 1, parseInt(dateArray[2]));
  51. return this.daysOfWeek[date.getDay()] + " " + date.getDate() + " " + this.monthsOfYear[date.getMonth()] + " " + date.getFullYear();
  52. }
  53. }