Add new '%s' format to d3.time.format for the ordinal date suffix. #495

Closed
wants to merge 1 commit into
from

Conversation

6 participants

jwhitley commented Feb 8, 2012

This change adds a new %s format to d3.time.format which prints the English ordinal suffix of the formatted date.

For example, d3.time.format("%b %e%s") used to format 01 Jan 1990 would output Jan 1st.

+ }
+
+ return suffix;
+}
@mbostock

mbostock Feb 19, 2012

Owner

I might reduce this like follows:

var d3_time_suffixes = ["th", "st", "nd", "rd"];

function d3_time_suffix(number) {
  var tail = number % 100;
  return d3_time_suffixes[tail < 11 || tail > 13 && tail % 10] || d3_time_suffixes[0];
}
Owner

mbostock commented Feb 19, 2012

Thanks for the contribution! I'm a bit worried about deviating from the "standard" strptime and strftime behavior, but I see there's precedent for this in PHP, django, and datejs… so it may be worthwhile to include.

Also, ideally, there's feature parity between formatting and parsing. So, if you add %s to the format, the parser should probably be able to ignore the ordinal suffix when you parse "Jan 1st", e.g.

👍 I agree with this addition!

+1

Yes please. Any update on this?

Owner

mbostock commented Feb 1, 2014

This implementation needs to be localized using the new code structure introduced in release 3.4.

Someone with knowledge of ordinal indicators in other languages should comment on whether this is a reasonable approach, and if not suggest a different suitable parameterized implementation. (Wikipedia has some information on ordinal indicators; it looks like some languages use prefixes rather than suffixes. At the least we should be capable of supporting FIGS, CJK and Russian.)

If having a array of up to ten ordinal suffixes (or prefixes?) is reasonable, then the implementation should also be abbreviated as described in my comment above.

@ghost

ghost commented Feb 1, 2014

Well I just asked some Russians in ##russian on freenode... they don't use date suffixes. Don't know if that helps.

bollwyvl commented Feb 1, 2014

Just poking around: moment.js handles this with a set of tokens for specific date components with ordinals: Mo, Do, etc. This goes beyond the approach of a "date suffix" token pretty significantly, so may not be entirely applicable. Due to this complexity, they accept either function(number, period) or a %-formatted string.

Here is a PR that solves some of these issues around Chinese which leads to one for Russian and Ukrainian.

Perusing their lang files around the 8 languages mentioned above, here's what i can extract for day-of-month:

  • French: function (number) { return number + (number === 1 ? 'er' : ''); }
  • Italian: %dº
  • German: %d.
  • Chinese: %d日
  • Japanese: N/A
  • Korean: %d일
  • Russian: %d-го

Hope this helps!

minikomi commented Feb 2, 2014

%d日 is accurate for Japanese also

On Sunday, February 2, 2014, bollwyvl notifications@github.com wrote:

Just poking around: moment.js handles this with a set of tokens for
specific date components with ordinals: Mo, Do, etc. This goes beyond the
approach of a "date suffix" token pretty significantly, so may not be
entirely applicable. Due to this complexity, they accept either function(number,
period) or a %-formatted string.

Here is a PR that solves some of these issues around Chinesehttps://github.com/moment/moment/pull/750which leads to one for Russian
and Ukrainian moment/moment#825.

Perusing their lang files around the 8 languages mentioned above, here's
what i can extract for day-of-month:

  • French: function (number) { return number + (number === 1 ? 'er' :
    ''); }
  • Italian: %dº
  • German: %d.
  • Chinese: %d日
  • Japanese: N/A
  • Korean: %d일
  • Russian: %d-го

Hope this helps!


Reply to this email directly or view it on GitHubhttps://github.com/mbostock/d3/pull/495#issuecomment-33876109
.

@ghost

ghost commented Apr 2, 2014

I really wish we could just get this into d3. It is common in other libraries, and I'm sure lots of you have managers/designers that require your date to be something like "April 2nd, 2014" not "April 2, 2014."

I feel like this has been held back for way too long, especially if its functioning, and we have other languages chiming in. And from what I here, the other languages may just not even have this, so could easily just have it be replaced with nothing, if not in a language that supports this.

@mbostock mbostock modified the milestones: 3.x, 4.0 Oct 22, 2015

@mbostock mbostock referenced this pull request in d3/d3-time-format Nov 10, 2015

Open

Ordinal dates? #10

1 of 6 tasks complete
Owner

mbostock commented Nov 10, 2015

I will be working on this for 4.0, over in the d3-time-format repo.

Owner

mbostock commented Jul 14, 2016

Superseded by d3/d3-time-format#10.

@mbostock mbostock closed this Jul 14, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment