Luxon: support for n-th token?

Created on 7 Dec 2017  路  3Comments  路  Source: moment/luxon

Is there an equivalent for 1st, 2nd, 3rd etc. for luxon? I was looking through the documentation, but couldn't find anything regarding this topic.

compare to:
http://momentjs.com/docs/#/displaying/format/ e.g. DDDo, Do, Mo, wo etc.

Most helpful comment

This function should do the trick (for English at least)

function getNumberSuffix(num) {
  const th = 'th'
  const rd = 'rd'
  const nd = 'nd'
  const st = 'st'

  if (num === 11 || num === 12 || num === 13) return th

  let lastDigit = num.toString().slice(-1)

  switch (lastDigit) {
    case '1': return st
    case '2': return nd
    case '3': return rd
    default:  return th
  }
}

All 3 comments

No, there's nothing for that. Luxon relies on Intl.NumberFormat to handle numbers, and it would need to support for nd, rd, th, etc in arbitrary locales, which it does not. When (and if) that API gains that capability, I will be able to add to Luxon too.

This function should do the trick (for English at least)

function getNumberSuffix(num) {
  const th = 'th'
  const rd = 'rd'
  const nd = 'nd'
  const st = 'st'

  if (num === 11 || num === 12 || num === 13) return th

  let lastDigit = num.toString().slice(-1)

  switch (lastDigit) {
    case '1': return st
    case '2': return nd
    case '3': return rd
    default:  return th
  }
}

Or you can do this 馃槵 (still for English)

function ordinal(n) {
  var s = ["th", "st", "nd", "rd"];
  var v = n%100;
  return n + (s[(v-20)%10] || s[v] || s[0]);
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

icambron picture icambron  路  5Comments

ycmjason picture ycmjason  路  4Comments

emanuelegalbiati picture emanuelegalbiati  路  3Comments

icambron picture icambron  路  5Comments

jonathanborges picture jonathanborges  路  6Comments