format-date.ts 772 B

123456789101112131415161718192021222324252627282930313233
  1. import moment from 'moment'
  2. moment.updateLocale('en', {
  3. calendar: {
  4. lastDay: '[Yesterday]',
  5. sameDay: '[Today]',
  6. nextDay: '[Tomorrow]',
  7. lastWeek: 'ddd, Do MMM YY',
  8. nextWeek: 'ddd, Do MMM YY',
  9. sameElse: 'ddd, Do MMM YY',
  10. },
  11. })
  12. export function formatTime(
  13. date: moment.MomentInput,
  14. format = 'h:mm a',
  15. utc = false
  16. ) {
  17. const momentDate = utc ? moment.utc(date) : moment(date)
  18. return momentDate.format(format)
  19. }
  20. export function relativeDate(date: moment.MomentInput) {
  21. return moment(date).calendar()
  22. }
  23. export function formatTimeBasedOnYear(date: moment.MomentInput) {
  24. const currentDate = moment()
  25. return currentDate.diff(date, 'years') > 0
  26. ? formatTime(date, 'D MMMM YYYY, h:mm a')
  27. : formatTime(date, 'D MMMM, h:mm a')
  28. }