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

Date: Add function to get relative time #14486

Closed
swissspidy opened this issue Mar 18, 2019 · 6 comments
Closed

Date: Add function to get relative time #14486

swissspidy opened this issue Mar 18, 2019 · 6 comments
Labels
[Package] Date /packages/date [Status] In Progress Tracking issues with work in progress [Type] Enhancement A suggestion for improvement.

Comments

@swissspidy
Copy link
Member

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 like human_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 similar from() 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.js

@swissspidy swissspidy added [Type] Enhancement A suggestion for improvement. [Package] Date /packages/date labels Mar 18, 2019
@iandunn
Copy link
Member

iandunn commented Sep 9, 2019

I'd find this functionality useful.

Alternatives would be using Moment.js directly and not via @wordpress/date

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?

...or using a dedicated library like timeago.js

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.

@swissspidy
Copy link
Member Author

I listed these two alternatives as options for someone who needs this kind of functionality but finds out that @wordpress/date lacks it.

Since that package already uses Moment.js it definitely makes sense to use that one for this enhancement as well.

@iandunn
Copy link
Member

iandunn commented Sep 9, 2019

👍

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

@swissspidy
Copy link
Member Author

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.

@TimothyBJacobs
Copy link
Member

This has come up again in the block directory. Right now we are returning a human_time_diff from the REST API, but ideally we'd just return the raw date value and Gutenberg would transform this client side.

@swissspidy
Copy link
Member Author

humanTimeDiff now exists

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Date /packages/date [Status] In Progress Tracking issues with work in progress [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants