aboutsummaryrefslogblamecommitdiffhomepage
path: root/client/src/app/helpers/utils/date.ts
blob: 75363cc8197d78440498e151838398b9b4f4eb24 (plain) (tree)
1
2

                                          























                                                                                              
















                                                                                                    

             


                   
import { DatePipe } from '@angular/common'

let datePipe: DatePipe
let intl: Intl.DateTimeFormat

type DateFormat = 'medium' | 'precise'

function dateToHuman (localeId: string, date: Date, format: 'medium' | 'precise' = 'medium') {
  if (!datePipe) {
    datePipe = new DatePipe(localeId)
  }

  if (!intl) {
    intl = new Intl.DateTimeFormat(localeId, {
      hour: 'numeric',
      minute: 'numeric',
      second: 'numeric',
      year: '2-digit',
      month: 'numeric',
      day: 'numeric',
      fractionalSecondDigits: 3
    })
  }

  if (format === 'medium') return datePipe.transform(date, format)
  if (format === 'precise') return intl.format(date)
}

function durationToString (duration: number) {
  const hours = Math.floor(duration / 3600)
  const minutes = Math.floor((duration % 3600) / 60)
  const seconds = duration % 60

  const minutesPadding = minutes >= 10 ? '' : '0'
  const secondsPadding = seconds >= 10 ? '' : '0'
  const displayedHours = hours > 0 ? hours.toString() + ':' : ''

  return (
    displayedHours + minutesPadding + minutes.toString() + ':' + secondsPadding + seconds.toString()
  ).replace(/^0/, '')
}

export {
  DateFormat,

  durationToString,
  dateToHuman
}