datesUtils.ts 866 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import moment from 'moment'
  2. export default class DatesUtils {
  3. private $moment: typeof moment;
  4. constructor (momentInstance: any) {
  5. this.$moment = momentInstance
  6. }
  7. /**
  8. * Formate la ou les dates au format donné
  9. *
  10. * @param dates
  11. * @param format
  12. */
  13. formattedDate (dates: any, format: string): string {
  14. const dFormat: Array<string> = []
  15. if (Array.isArray(dates)) {
  16. for (const date of dates) {
  17. dFormat.push(this.$moment(date).format(format))
  18. }
  19. } else {
  20. dFormat.push(this.$moment(dates as string).format(format))
  21. }
  22. return dFormat.join(' - ')
  23. }
  24. /**
  25. * Trie les dates par ordre chronologique
  26. * @param dates
  27. */
  28. sortDate (dates: Array<string>): Array<string> {
  29. return dates.sort((a, b) => {
  30. if (a > b) { return 1 }
  31. if (a < b) { return -1 }
  32. return 0
  33. })
  34. }
  35. }