Moment.js 从现在/今天(几天前、几个月前和几年前)
Moment.js from now / today in Days, Months & Years Ago
我正在使用 moment.js 来显示从 'today' 到发布 post(s) 所经过的时间,对于任何具有原始 ISO 日期的 post '.iso-date'然后从post输出时间量到div'.display-date'。我有一个运行正常的脚本,但它只显示 'years',如果 post 是今天,那么它什么都不输出。同样,它只显示前 12 个月后的月份和年份,即 1-11 个月,然后仅显示 x 年前。
当前输出'Posted 3 months ago'、'Posted a year ago'、'Posted e years ago'等
所以我想看到的是 'Posted Just Now'、'Posted Today'、'Posted 6 days ago'、'Posted 3 weeks, 5 days ago'、9 个月、3 周、5 天前发布'和'Posted 3 years, 9 months, 3 weeks, 5 days ago ' - 或者只是 'the nearest date interval'.
标记示例
<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>
这是当前脚本:-
// 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);
}
});
});
如果您能就如何使用 Moment.js r 实现这一点,我将不胜感激,也许没有任何脚本依赖性 - jQuery / JavaScript.
谢谢
格兰尼男孩
检查以下片段。我从这里使用了这个 answer 并根据您的要求实现了它。
function dateAgo(date) {
var startDate = new Date( date + " " + '12:00 AM');
var endDate = new Date();
var diffDate = new Date(new Date() - startDate);
var yearDiff = diffDate.toISOString().slice(0, 4) - 1970;
var monthDiff = diffDate.getMonth();
var dayDiff = diffDate.getDate()-1;
var msec = endDate - startDate;
var mins = Math.floor(msec / 60000);
var hrs = Math.floor(mins / 60);
var days = Math.floor(hrs / 24);
var label = 'Posted ';
if( mins <= 30 ){
label += ' Just Now';
}else if( hrs <= 24 ){
label += ' Just Today';
}else{
if( yearDiff > 0 ){
label += ( yearDiff > 1 ) ? yearDiff+' years, ' : yearDiff+' year, ';
}
if( monthDiff > 0 ){
label += ( monthDiff > 1 ) ? monthDiff+' months, ' : monthDiff+' month, ';
}
if( dayDiff > 0 ){
label += ( dayDiff > 1 ) ? dayDiff+' days ' : dayDiff+' day ';
}
label += 'ago';
}
return label;
}
(function() {
var elements = document.body.getElementsByClassName('iso-date');
for (var i = 0; i < elements.length; i++) {
var date = elements[i].innerHTML;
var label = dateAgo(date);
elements[i].nextElementSibling.innerHTML = label;
}
})();
<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">2021-04-06</div>
<div class="display-date"></div>
</div>
我正在使用 moment.js 来显示从 'today' 到发布 post(s) 所经过的时间,对于任何具有原始 ISO 日期的 post '.iso-date'然后从post输出时间量到div'.display-date'。我有一个运行正常的脚本,但它只显示 'years',如果 post 是今天,那么它什么都不输出。同样,它只显示前 12 个月后的月份和年份,即 1-11 个月,然后仅显示 x 年前。
当前输出'Posted 3 months ago'、'Posted a year ago'、'Posted e years ago'等
所以我想看到的是 'Posted Just Now'、'Posted Today'、'Posted 6 days ago'、'Posted 3 weeks, 5 days ago'、9 个月、3 周、5 天前发布'和'Posted 3 years, 9 months, 3 weeks, 5 days ago ' - 或者只是 'the nearest date interval'.
标记示例
<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>
这是当前脚本:-
// 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);
}
});
});
如果您能就如何使用 Moment.js r 实现这一点,我将不胜感激,也许没有任何脚本依赖性 - jQuery / JavaScript.
谢谢
格兰尼男孩
检查以下片段。我从这里使用了这个 answer 并根据您的要求实现了它。
function dateAgo(date) {
var startDate = new Date( date + " " + '12:00 AM');
var endDate = new Date();
var diffDate = new Date(new Date() - startDate);
var yearDiff = diffDate.toISOString().slice(0, 4) - 1970;
var monthDiff = diffDate.getMonth();
var dayDiff = diffDate.getDate()-1;
var msec = endDate - startDate;
var mins = Math.floor(msec / 60000);
var hrs = Math.floor(mins / 60);
var days = Math.floor(hrs / 24);
var label = 'Posted ';
if( mins <= 30 ){
label += ' Just Now';
}else if( hrs <= 24 ){
label += ' Just Today';
}else{
if( yearDiff > 0 ){
label += ( yearDiff > 1 ) ? yearDiff+' years, ' : yearDiff+' year, ';
}
if( monthDiff > 0 ){
label += ( monthDiff > 1 ) ? monthDiff+' months, ' : monthDiff+' month, ';
}
if( dayDiff > 0 ){
label += ( dayDiff > 1 ) ? dayDiff+' days ' : dayDiff+' day ';
}
label += 'ago';
}
return label;
}
(function() {
var elements = document.body.getElementsByClassName('iso-date');
for (var i = 0; i < elements.length; i++) {
var date = elements[i].innerHTML;
var label = dateAgo(date);
elements[i].nextElementSibling.innerHTML = label;
}
})();
<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">2021-04-06</div>
<div class="display-date"></div>
</div>