-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Date: Add function to get relative time #14486
Comments
I'd find this functionality useful.
That's a fair point, though; I guess the benefit of having a wrapper would be to abstract away Moment in case we want to replace it w/ another library in the future? Are there other pros/cons to consider?
What would the benefits of that be? At a quick glance, it seems like standardizing on Moment would be more performant and maintainable than adding a separate library. |
I listed these two alternatives as options for someone who needs this kind of functionality but finds out that Since that package already uses Moment.js it definitely makes sense to use that one for this enhancement as well. |
👍 For anyone looking to use Moment directly in the meantime, here's an example: /**
* External dependencies
*/
import moment from 'moment';
// ...
render() {
const { exampleTime } = this.props(); // a timestamp in milliseconds, or something else parseable by Moment
return (
<span>
It happened { moment( exampleTime ).fromNow() }.
</span>
)
} |
Thanks for sharing the example! The nice thing about using the date package for that would be that one would automatically benefit from the existing localization in WordPress. See also #16728. |
This has come up again in the block directory. Right now we are returning a |
|
Is your feature request related to a problem? Please describe.
@wordpress/date
, powered by Moment.js under the hood, is a neat and simple way to format dates. However, what currently is missing is a way to display relative times likehuman_time_diff()
does in PHP.Describe the solution you'd like
Moment.js has a function called
fromNow()
(docs) that allows you to display relative times like "4 years ago" or "44 seconds ago", and a similarfrom()
function to display a moment in relation to a time other than now.A new function similar to
human_time_diff()
could be added to the package that makes use of these functions.Happy to work on a PR to add this kind of functionality.
Describe alternatives you've considered
Alternatives would be using Moment.js directly and not via
@wordpress/date
, or using a dedicated library like timeago.jsThe text was updated successfully, but these errors were encountered: