获取UNIX时间戳,例如:1372108039,已经过了一段时间(这是从昨天开始)。
我想要的是将其改为“1天前”或“15小时前”的内容。
我在Twitter插件中找到了以下代码,现在花费时间,时间戳,找到差异并相应地输出。我有一个想法,它可以简单地在JQuery中使用下面的代码作为基础,但不知道从哪里开始。
任何帮助都将不胜感激。
function(dateString) {
var rightNow = new Date();
var then = 1372108039;
var diff = rightNow - then;
var second = 1000,
minute = second * 60,
hour = minute * 60,
day = hour * 24,
week = day * 7;
if (isNaN(diff) || diff < 0) {
return ""; // return blank string if unknown
}
if (diff < second * 2) {
// within 2 seconds
return "right now";
}
if (diff < minute) {
return Math.floor(diff / second) + " seconds ago";
}
if (diff < minute * 2) {
return "about 1 minute ago";
}
if (diff < hour) {
return Math.floor(diff / minute) + " minutes ago";
}
if (diff < hour * 2) {
return "about 1 hour ago";
}
if (diff < day) {
return Math.floor(diff / hour) + " hours ago";
}
if (diff > day && diff < day * 2) {
return "yesterday";
}
if (diff < day * 365) {
return Math.floor(diff / day) + " days ago";
}
else {
return "over a year ago";
}
}, // timeAgo()