使用 jQuery(或类似)的 timeAgo 来显示用户(客户端)进入或刷新页面的时间
using timeAgo by jQuery (or similar) to show the time ago a user (client) enters or refreshes a page
目前这是结果,但计时器不会转到“大约一分钟前”或“5 分钟前”等。它始终采用实际时间。我没有找到适合我的解决方案。
//timeago by jQuery
(function timeAgo(selector) {
var templates = {
prefix: "",
suffix: " ago",
seconds: "less than a minute",
minute: "about a minute",
minutes: "%d minutes",
hour: "about an hour",
hours: "about %d hours",
day: "a day",
days: "%d days",
month: "about a month",
months: "%d months",
year: "about a year",
years: "%d years"
};
var template = function(t, n) {
return templates[t] && templates[t].replace(/%d/i, Math.abs(Math.round(n)));
};
var timer = function(time) {
if (!time)
return;
time = time.replace(/\.\d+/, ""); // remove milliseconds
time = time.replace(/-/, "/").replace(/-/, "/");
time = time.replace(/T/, " ").replace(/Z/, " UTC");
time = time.replace(/([\+\-]\d\d)\:?(\d\d)/, " "); // -04:00 -> -0400
time = new Date(time * 1000 || time);
var now = new Date();
var seconds = ((now.getTime() - time) * .001) >> 0;
var minutes = seconds / 60;
var hours = minutes / 60;
var days = hours / 24;
var years = days / 365;
return templates.prefix + (
seconds < 45 && template('seconds', seconds) ||
seconds < 90 && template('minute', 1) ||
minutes < 45 && template('minutes', minutes) ||
minutes < 90 && template('hour', 1) ||
hours < 24 && template('hours', hours) ||
hours < 42 && template('day', 1) ||
days < 30 && template('days', days) ||
days < 45 && template('month', 1) ||
days < 365 && template('months', days / 30) ||
years < 1.5 && template('year', 1) ||
template('years', years)
) + templates.suffix;
};
var elements = document.getElementsByClassName('timeago');
for (var i in elements) {
var $this = elements[i];
if (typeof $this === 'object') {
$this.innerHTML = timer($this.getAttribute('title') || $this.getAttribute('datetime'));
}
}
// update time every minute
setTimeout(timeAgo, 60000);
})();
//Uhrzeit
a = new Date ();
b = a.getHours();
c = a.getMinutes();
d = a.getSeconds();
zeit = b+':'+c+':'+d;
document.getElementById("uhrzeit").innerHTML = zeit;
这就是我想在我的 html 文档中使用它的方式,当我设置日期时,或者当我使用“uhrzeit”时,它会起作用,这是一个需要实际时间的小脚本,但我想要客户端打开页面的时间。不便之处敬请谅解
<div class="card-footer">
<small class="text-muted"><time class="timeago" datetime="uhrzeit">December 17, 2019</time></small>
</div>
可能你的错误是在这些行中:
for (var i in elements) {
var $this = elements;
if (typeof $this === 'object') {
$this.innerHTML = timer($this.getAttribute('title') || $this.getAttribute('datetime'));
}
}
您有没有更改标题或日期时间属性?因为,如果不是,您总是将相同的值发送到 timer
函数
编辑:
添加用户最后一次输入的时间作为 time
元素的属性。
像这样
<time class="timeago" datetime="2008-07-17T09:24:17Z" title="July 17, 2008">11 years ago</time>
目前这是结果,但计时器不会转到“大约一分钟前”或“5 分钟前”等。它始终采用实际时间。我没有找到适合我的解决方案。
//timeago by jQuery
(function timeAgo(selector) {
var templates = {
prefix: "",
suffix: " ago",
seconds: "less than a minute",
minute: "about a minute",
minutes: "%d minutes",
hour: "about an hour",
hours: "about %d hours",
day: "a day",
days: "%d days",
month: "about a month",
months: "%d months",
year: "about a year",
years: "%d years"
};
var template = function(t, n) {
return templates[t] && templates[t].replace(/%d/i, Math.abs(Math.round(n)));
};
var timer = function(time) {
if (!time)
return;
time = time.replace(/\.\d+/, ""); // remove milliseconds
time = time.replace(/-/, "/").replace(/-/, "/");
time = time.replace(/T/, " ").replace(/Z/, " UTC");
time = time.replace(/([\+\-]\d\d)\:?(\d\d)/, " "); // -04:00 -> -0400
time = new Date(time * 1000 || time);
var now = new Date();
var seconds = ((now.getTime() - time) * .001) >> 0;
var minutes = seconds / 60;
var hours = minutes / 60;
var days = hours / 24;
var years = days / 365;
return templates.prefix + (
seconds < 45 && template('seconds', seconds) ||
seconds < 90 && template('minute', 1) ||
minutes < 45 && template('minutes', minutes) ||
minutes < 90 && template('hour', 1) ||
hours < 24 && template('hours', hours) ||
hours < 42 && template('day', 1) ||
days < 30 && template('days', days) ||
days < 45 && template('month', 1) ||
days < 365 && template('months', days / 30) ||
years < 1.5 && template('year', 1) ||
template('years', years)
) + templates.suffix;
};
var elements = document.getElementsByClassName('timeago');
for (var i in elements) {
var $this = elements[i];
if (typeof $this === 'object') {
$this.innerHTML = timer($this.getAttribute('title') || $this.getAttribute('datetime'));
}
}
// update time every minute
setTimeout(timeAgo, 60000);
})();
//Uhrzeit
a = new Date ();
b = a.getHours();
c = a.getMinutes();
d = a.getSeconds();
zeit = b+':'+c+':'+d;
document.getElementById("uhrzeit").innerHTML = zeit;
这就是我想在我的 html 文档中使用它的方式,当我设置日期时,或者当我使用“uhrzeit”时,它会起作用,这是一个需要实际时间的小脚本,但我想要客户端打开页面的时间。不便之处敬请谅解
<div class="card-footer">
<small class="text-muted"><time class="timeago" datetime="uhrzeit">December 17, 2019</time></small>
</div>
可能你的错误是在这些行中:
for (var i in elements) {
var $this = elements;
if (typeof $this === 'object') {
$this.innerHTML = timer($this.getAttribute('title') || $this.getAttribute('datetime'));
}
}
您有没有更改标题或日期时间属性?因为,如果不是,您总是将相同的值发送到 timer
函数
编辑:
添加用户最后一次输入的时间作为 time
元素的属性。
像这样
<time class="timeago" datetime="2008-07-17T09:24:17Z" title="July 17, 2008">11 years ago</time>