I am using moment.js to show the time elapsed from 'today' to when a post(s) was published, for any post with the original ISO date '.iso-date' and then output the amount of time from the post in a div '.display-date'. I have a script that works ok, but it only shows 'years' and if the post is today then it outputs nothing. Similarly, it only shows months and then years after the first 12 months, so 1-11 months and then only x years ago.
Current output 'Posted 3 months ago', 'Posted a year ago', 'Posted e years ago' etc
So what I want to see is something like 'Posted Just Now', 'Posted Today', 'Posted 6 days ago', 'Posted 3 weeks, 5 days ago', Posted 9 months, 3 weeks, 5 days ago' and 'Posted 3 years, 9 months, 3 weeks, 5 days ago ' - or perhaps just 'the nearest date interval'.
Markup Example
<div class="posts-list">
<div class="grid-block">
<div class="iso-date">2019-09-30</div>
<div class="display-date"></div>
</div>
<div class="grid-block">
<div class="iso-date">2020-07-12</div>
<div class="display-date"></div>
</div>
</div>
Here is the current script:-
// Moment
$(document).ready(function () {
// Moment targets
var publishedDate = ".iso-date";
var displayDate = ".display-date";
$(publishedDate).each(function (i, e) {
var p = $(e).text();
var a = $(e).next(displayDate);
if (moment(p).isValid()) {
var m = moment(new Date(p));
var s = moment(new Date(m)).fromNow();
// var s = moment(new Date(m)).diff(moment(), 'milliseconds');
a.text("Posted " + s);
}
});
});
I would appreciate your input on how to achieve this with Moment.js r perhaps without any script dependencies - jQuery / JavaScript.
Thanks
Glennyboy