Simplify Relative Timestamps in React with use-relative-time

use-relative-time is a tiny React hook for formatting relative dates and times. It provides a simple way to convert timestamps into human-readable strings like “5 minutes ago” or “in 3 hours”.

By leveraging the Intl.RelativeTimeFormat API, useRelativeTime can localize its output to match the user’s locale.

How to use it:

1. Install and import the useRelativeTime hook.

# NPM
$ npm i @nkzw/use-relative-time
import useRelativeTime from '@nkzw/use-relative-time';

2. Format a date relative to the current time:

const timeAgo = useRelativeTime(time);
return (
  <time>{timeAgo}</time>
);

Download Details:

Author: nkzw-tech

Live Demo: View The Demo

Download Link: Download The Source Code

Official Website: https://github.com/nkzw-tech/use-relative-time

License: MIT

Add Comment