datesUtils.ts 861 B

1234567891011121314151617181920212223242526272829303132333435363738
  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. * @param dates
  10. * @param format
  11. */
  12. formattedDate (dates: any, format: string): string {
  13. const dFormat: Array<string> = []
  14. if (Array.isArray(dates)) {
  15. for (const date of dates) {
  16. dFormat.push(this.$moment(date).format(format))
  17. }
  18. } else {
  19. dFormat.push(this.$moment(dates as string).format(format))
  20. }
  21. return dFormat.join(' - ')
  22. }
  23. /**
  24. * Trie les dates par ordre chronologique
  25. * @param dates
  26. */
  27. sortDate (dates: Array<string>): Array<string> {
  28. return dates.sort((a, b) => {
  29. if (a > b) { return 1 }
  30. if (a < b) { return -1 }
  31. return 0
  32. })
  33. }
  34. }