using mongoose createdAt to render item in React based off the time posted

Viewed 18

I have a post which displays the time, which is accessed by the createdAt timestamp from mongoDB. I then convert the createtAt timestamp using the moment library. When I create a new post, I want a span badge to appear next to it only if it has been posted within the last 24 hours. I have tried using the .includes() like so:

//for example if a post is posted "2 hours ago" or "19 hours ago" it will show the span tag
{moment(post.createdAt).fromNow().includes("hours") && (
                <span className="new-post">new post</span>
              )}

however, I believe that this is not so practical. Also, this method^ will not show the <span></span> tag if it was posted seconds or minutes ago (which is crucial). Is there a way to get this done more efficiently?

0 Answers
Related