Use of timeago function in React JS

Timeago function use for show difference between current date timestamp and old timestamp. today i am going to show you how to use time ago function for show posted time for example “2 days ago” etc. There is two methods for show ago time.

1. With help of library “react-timeago”
2. With custom created component

1. With help of library “react-timeago”

In React js project we can use react-time-ago library for show time. for use library you should first install library in your project with this command. follow this link for use this library https://www.npmjs.com/package/react-timeago

Then require this library in your file and use this code for show ago time.

2. And second method is create your own function and use in a component.

This is my way use of this code. you can use this function according to your way.  First you need a another common.js file and create a custom function. I created this file because of reuse this function in multiple files.

Common.js file in src/component/ folder

After that require this component in another component for use this function like this code:

Now you can see result on your screen as time ago format like that.

Output: 2 days ago

This is the function for show time ago format. so if you like this article please share and comment. Thank you, Happy learning  🙂

 

Tags:

Add a Comment

Your email address will not be published. Required fields are marked *