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

Relative time: Additional features #391

Open
rxaviers opened this issue Feb 12, 2015 · 2 comments
Open

Relative time: Additional features #391

rxaviers opened this issue Feb 12, 2015 · 2 comments

Comments

@rxaviers
Copy link
Member

Support relative times:

  • in the past , eg "5 days ago";
  • in the future, eg "in 5 days";

API

Globalize.formatRelativeTime( value[, unit][, options] )

Examples:

.formatRelativeTime( inTwoDays ); // in 2 days
.formatRelativeTime( twoDaysAgo ); // 2 days ago
.formatRelativeTime( 2, "day" ); // in 2 days
.formatRelativeTime( twoDaysAgo, "hour" ); // 48 hours ago
.formatRelativeTime( -48, "hour" ); // 48 hours ago
.formatRelativeTime( twoDaysAgo, "hour", { form: "short" } ); // 48 hr. ago
.formatRelativeTime({ from: christmas2001, to: newyear2002 }, "day" ); // in 7 days

Value: Object with from and to keys and Date instance values, or a Date instance (considered to be the to value and from defaults to now (new Date())), or a Number (positive numbers represent future, negative numbers represent past).

Unit: Optional when value is a Date instance or an object of Date instances. Required when value is a Number. Possible values are (year, quarter, month, week, day, hour, minute, and second), e.g., "in 30 days", "in 4 weeks", or "in 1 month", ... Defaults to automatic detection when using Date instances, therefore implementing a simple approximation algorithm [1]. But, delegating any more complex things to a specific date manipulation library such as moment.js.

Options:

  • form (short, narrow), eg. "in 30 days", "in 4 wk.", or "in 1 mo."...

1:

  • "1 second ago" .. "59 seconds ago"; (1 .. 59 s)
  • "1 minute ago" .. "59 minutes ago"; (60 .. 60*60 s)
  • "1 hour ago" .. "59 hours ago";
  • "1 day ago" .. "59 days ago";
  • "1 week ago" .. "59 weeks ago";
  • "1 month ago" .. "59 months ago";
  • "1 year ago" .. "59 years ago";

(analogous for future)

CLDR

Relative time fields can be found on /dateFields.json.

Other

References:

This issue succeeds #205 (discussion to get to the API above). For absolute durations, see .formatUnit() in #252.

@rxaviers rxaviers added this to the 1.1.0 milestone Feb 12, 2015
@rxaviers
Copy link
Member Author

On CLDR v26, different widths/forms appeared: short and narrow. So, I've updated the API accordingly.

I've also (a) made the API more similar to .formatUnit(), i.e., making unit an argument and having options.form to modify the default width/form; and (b) allowed value to be a Number, whose sign represents the past/future direction (according to #389 (comment)).

rxaviers pushed a commit that referenced this issue Mar 26, 2015
- Only support numbers as value parameter for the moment.

Ref #391
Closes #395
rxaviers added a commit that referenced this issue Mar 26, 2015
- Style updates.
- Add default CLDR validation.
- Minor edits.

Amends 47cbe1207a30ac8d3ba9b0ab3ef8a6107e2b421a
Ref #391
Ref #395
@rxaviers
Copy link
Member Author

Ecma proposal:

@rxaviers rxaviers changed the title Support relative time Relative time: Additional features Oct 10, 2015
@rxaviers rxaviers modified the milestone: 1.1.0 Oct 11, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant