0

someone can help me, i have a date of coment with result:

June 10, 2022, 3:29 PM

i want to convert if the date coment same with the day now, value:

14 hours, 27 minutes ago

when the date coment not same with day now:

2 day, 15 hours ago

Wandi
  • 27
  • 1
  • 8

3 Answers3

1

Use Moment.js fromNow() this will produce your desired result!

moment([2007, 0, 29]).fromNow();     // 4 years ago
moment([2007, 0, 29]).fromNow(true); // 4 years
Naren Murali
  • 19,250
  • 3
  • 27
  • 54
0

Checkout for moment library to convert the date to the hours/minutes ago or day/hours ago.

Hitend
  • 1
  • 3
  • 1
    Hi Hitend, welcome to SO! Good to see that you made a first contribution, but try to add some working code when providing an answer. Make it as concrete as possible by providing an example. If you just want to hint that there is a library somebody could look at, a comment on the post is more suitable than an answer. – Axel Köhler Jul 07 '22 at 13:29
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 07 '22 at 13:29
  • Hi all, thank you @AxelKöhler for your warm welcome, I always tend to provide answers in question format. If you know what I am hinting at. Sure will keep in mind that the library section should be in the comments. – Hitend Jul 08 '22 at 10:08
0

Check out date-fns library. It is lightweight.

https://date-fns.org/docs/Getting-Started

some examples

import { format, formatDistance, formatRelative, subDays } from 'date-fns'

format(new Date(), "'Today is a' eeee")
//=> "Today is a Thursday"

formatDistance(subDays(new Date(), 3), new Date(), { addSuffix: true })
//=> "3 days ago"

formatRelative(subDays(new Date(), 3), new Date())
//=> "last Friday at 7:26 p.m."
Abhishek Shukla
  • 646
  • 1
  • 8
  • 21