Home > Software engineering >  How can I display time like 30 seconds ago or 2 hour ago and update up to days in react native
How can I display time like 30 seconds ago or 2 hour ago and update up to days in react native

Time:05-25

Hi there I am developing a social application I want to display passed time i.e when the post was created like Facebook for examples created 30 seconds ago or created 2 hour ago and up to days in react-native

CodePudding user response:

You can use enter image description here

CodePudding user response:

In my opinion better library for updating date is dayjs or date-fns. Both libraries are light weight. Specially if you are creating social app where I assume you would like to render a lot of dates.

https://date-fns.org/

https://day.js.org/ In dayjs what are you looking for is: dayjs(someDate).fromNow() from is output like this: 3 minutes ago

CodePudding user response:

I have found my answer. It was a single line and easy using 'moment' library here is how I got it:

 let createdAt = moment(moment.now()).fromNow(); 

for display

<Text> Createdat: {createdAt} </Text>

  • Related