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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. static isWeekend(date: Date) {
  42. return date.getDay() === 6 || date.getDay() === 0;
  43. }
  44. getMonthsOfYear() {
  45. return this.monthsOfYear;
  46. }
  47. /**
  48. * Gets a translated string representing the given date.
  49. *
  50. * @param dateString The date with the format YYYY-MM-DD
  51. * @return {string} The translated string
  52. */
  53. getTranslatedDate(dateString: string) {
  54. let dateArray = dateString.split('-');
  55. let date = new Date();
  56. date.setFullYear(parseInt(dateArray[0]), parseInt(dateArray[1]) - 1, parseInt(dateArray[2]));
  57. return this.daysOfWeek[date.getDay()] + " " + date.getDate() + " " + this.monthsOfYear[date.getMonth()] + " " + date.getFullYear();
  58. }
  59. }