Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get weeks/ days/ hours/ minutes ago from custom date & time "from now" in React Native?

I have a date & time format: 2019-11-25 09:49:19. How can we compare this from current date & time and can display like: 3 days ago or weeks/ hours/ min ago

like image 855
Luvnish Monga Avatar asked Nov 28 '19 12:11

Luvnish Monga


2 Answers

After many hit and trails I found exact solution:

moment.utc("2019-12-04 12:00:24").local().startOf('seconds').fromNow()

Output:

30 minutes ago, 1 hour ago, 2 weeks ago

like image 128
Luvnish Monga Avatar answered Sep 20 '22 16:09

Luvnish Monga


You can use moment.js library. moment.js

moment("20111031", "YYYYMMDD").fromNow(); // 8 years ago
moment("20120620", "YYYYMMDD").fromNow(); // 7 years ago
moment().startOf('day').fromNow();        // 16 hours ago
moment().endOf('day').fromNow();          // in 8 hours
moment().startOf('hour').fromNow();       // an hour ago
like image 21
octobus Avatar answered Sep 19 '22 16:09

octobus