Application Android et IOS pour l'amicale des élèves https://play.google.com/store/apps/details?id=fr.amicaleinsat.application
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.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. if (DateManager.instance == null) DateManager.instance = new DateManager();
  38. return DateManager.instance;
  39. }
  40. static isWeekend(date: Date): boolean {
  41. return date.getDay() === 6 || date.getDay() === 0;
  42. }
  43. getMonthsOfYear(): Array<string> {
  44. return this.monthsOfYear;
  45. }
  46. /**
  47. * Gets a translated string representing the given date.
  48. *
  49. * @param dateString The date with the format YYYY-MM-DD
  50. * @return {string} The translated string
  51. */
  52. getTranslatedDate(dateString: string): string {
  53. const dateArray = dateString.split('-');
  54. const date = new Date();
  55. date.setFullYear(
  56. parseInt(dateArray[0], 10),
  57. parseInt(dateArray[1], 10) - 1,
  58. parseInt(dateArray[2], 10),
  59. );
  60. return `${this.daysOfWeek[date.getDay()]} ${date.getDate()} ${
  61. this.monthsOfYear[date.getMonth()]
  62. } ${date.getFullYear()}`;
  63. }
  64. }