Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for n-th token? #118

Closed
dnalborczyk opened this issue Dec 7, 2017 · 10 comments
Closed

support for n-th token? #118

dnalborczyk opened this issue Dec 7, 2017 · 10 comments

Comments

@dnalborczyk
Copy link

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.

@icambron
Copy link
Member

icambron commented Dec 7, 2017

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.

@icambron icambron closed this as completed Dec 7, 2017
@0livare
Copy link

0livare commented Oct 10, 2020

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
  }
}

@Ciboulette
Copy link

Ciboulette commented Feb 18, 2021

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]);
}

@SlyDave
Copy link

SlyDave commented Apr 16, 2021

This is supported via Intl.PluralRules setting it to mode: ordinal ?
example for n-th token: https://v8.dev/features/intl-pluralrules

@icambron
Copy link
Member

Nifty. Yeah, I would take a PR that uses that to implement some new formatting/parsing tokens

@cjolif
Copy link

cjolif commented Dec 16, 2021

FYI I'll try to issue a PR about this in the coming weeks. So if someone else works on that, let me know :)

@cjolif
Copy link

cjolif commented Dec 16, 2021

Hmm actually Intl.PluralRules does give us the rule to apply based on the locale but not the actual formatting based on the rule. So this would still mean adding to luxon a map from the plural rules to the different languages formats. I suspect this is not what you had in mind @icambron? So we still have to wait on that?

@SlyDave
Copy link

SlyDave commented Dec 16, 2021

Hmm actually Intl.PluralRules does give us the rule to apply based on the locale but not the actual formatting based on the rule.

True, but you have Intl available so all that needs to happen is to pass the result to that to change local afterwards, no?

First work out the resulting rule from PluralRules, map to English st, nd, rd, th. Then plug into Intl. Or see if the Intl already contains the mapping for the one/two/many/other for each language?

Feels like a large ommison of Intl otherwise... requiring all users to implement the same boiler plate mapping

@cjolif
Copy link

cjolif commented Dec 16, 2021

map to English st, nd, rd, th.

That's where the issue is? Luxon would have to contain that mapping code for "all" locales? (Intl does not have that mapping as far as I can see but would be happy to be proven wrong!).

@icambron
Copy link
Member

icambron commented Dec 16, 2021

OK, I spent some more time here. @cjolif is correct. The PluralRules would let us figure out which of the strings to use in what situation, but not what the strings actually are. Maintaining those strings is ICU's job, not Luxon's. We're not going to add scores of these:

const suffixes = new Map([
  ['one',   'st'],
  ['two',   'nd'],
  ['few',   'rd'],
  ['other', 'th'],
]);

I believe Intl.NumberFormat should add an option for this...In the meantime, this issue isn't going anywhere

eoinkelly added a commit to ackama/projectworks-leave-notifications that referenced this issue Jan 9, 2023
Luxon does not support ordinal date formating (1st, 2nd, 3rd etc.)
because it is not supported by the underlying `Intl.DateTimeFormat`.
There is an open issue: moment/luxon#118
eoinkelly added a commit to ackama/projectworks-leave-notifications that referenced this issue Jan 9, 2023
Luxon does not support ordinal date formating (1st, 2nd, 3rd etc.)
because it is not supported by the underlying `Intl.DateTimeFormat`.
There is an open issue: moment/luxon#118
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants