K
K
kirill2709992019-12-01 20:20:16
JavaScript
kirill270999, 2019-12-01 20:20:16

How to display the publication date using js. by type, posted today, an hour ago, a day ago?

Hello! Please tell me how to implement the output of the publication date, which will change each time something like (published 20 minutes ago; 1 hour ago; a day ago), etc. in which direction to look for the answer? What a day I've been fighting. Thanks in advance.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Andrew Hecc, 2019-12-01
@Hecc

https://momentjs.com/
Use moment or its analogues, which are not few on the market. The moment is one of the most convenient.

K
Karpion, 2019-12-02
@Karpion

First you need to calculate the difference between that date and the current time. The easiest way is to convert both dates into something like "number of milliseconds since Unix epoch" Then we have to decide which interval we round to minutes, which to hours, etc. Well, round it up.

A
Andrey Khokhlov, 2019-12-02
@andrhohlov

https://timeago.org/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question