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

Ordinal day 1th 2nd,etc #163

Closed
amreniouinnovent opened this issue Jul 28, 2019 · 4 comments
Closed

Ordinal day 1th 2nd,etc #163

amreniouinnovent opened this issue Jul 28, 2019 · 4 comments

Comments

@amreniouinnovent
Copy link

I wonder if you could support ordinal number for the day of the month
1st, 2nd,3rd,4th, 21st, etc

I used a function but I think DateFormat should support it

  String today() {
    final DateTime now = DateTime.now();
    String suffix = 'th';
    final int digit = now.day % 10;
    if ((digit > 0 && digit < 4) && (now.day < 11 || now.day > 13)) {
      suffix = <String>['st', 'nd', 'rd'][digit - 1];
    }
    return DateFormat("E, MMM d'$suffix'").format(now); // 'Sun, Jun 30th'
  }
@alan-knight
Copy link
Contributor

We probably won't do this directly in Dart, as the current formatting is implemented. The underlying facilities in browsers are better now, so we're likely to add things by making them pass through to JS Intl in browsers, and to ICU or operating system libraries on other platforms.

@amreniouinnovent
Copy link
Author

amreniouinnovent commented Jul 30, 2019

@alan-knight As you mentioned it won't be implemented directly in intl I opened an issue in the Flutter repo
flutter/flutter#37221
I will close this issue for now.
If you want to improve the proposal on the flutter repo please put your ideas there.
Thank you.

@suztomo
Copy link

suztomo commented Oct 16, 2020

I have the same need for this for showing birthdays. Given a number, I want to show "st", "nd", "rd", or "th". For example,
"1st birthday", "2nd birthday", "3rd birthday" ... "11th birthday", ... "21st birthday", ... "101st birthday".

String ordinalAge(int age) {
  if (I18n.locale == const Locale('ja')) {
    // No ordinal number for Japanese
    return '$age';
  }
  if (age == 11 || age == 12 || age == 13) {
    return '${age}th';
  }
  if (age % 10 == 1) {
    return '${age}st';
  }
  if (age % 10 == 2) {
    return '${age}nd';
  }
  if (age % 10 == 3) {
    return '${age}rd';
  }
  return '${age}th';
}

@AlexanderFarkas
Copy link

It seems reasonable to implement it in dart. Web does ordinals better, but what about mobile - is calling native Kotlin/Swift option?
Also, ordinals are not magic, it's pure calculus.

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

4 participants