Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@skava/utils / src / toPrettyDate.ts
Size: Mime:
import { toDate } from 'exotic'
import format from 'date-fns/format'
import isThisWeek from 'date-fns/is_this_week'
import distanceInWordsToNow from 'date-fns/distance_in_words_to_now'

const DATE_FORMAT = 'YYYY-MM-DD h:mm A'
const DATE_ISO_FORMAT = 'YYYY-MM-DD hh:mm:ss a ZZ'

const toDateFormat = (formatOption: string) => (date: string) =>
  format(date, formatOption)

const toLongDate = toDateFormat(DATE_FORMAT)
const toISODate = toDateFormat(DATE_ISO_FORMAT)
const toTimeAgoInWords = (date: string) => distanceInWordsToNow(date)

// @todo - this isn't proper for parsing the data given by apis
// may need getActualDate
function toPrettyDate(date: string) {
  date = toDate(date)

  if (isThisWeek(date)) {
    return toTimeAgoInWords(date)
  } else {
    return toLongDate(date)
  }
}

export { toPrettyDate }