日期:2014-05-16 浏览次数:20367 次
从数字库中读一个timestamp的值,结果用json输出到前台变成了13位的毫秒。索性就以这个参数来算一下逆计时了。
?
效果:几秒前,几分钟前,几小时前
?
?
/**
参数为13位毫秒时间,也就是new Date().getTime()的格式。
**/
function getTimeBefore(time) {
??? var ret = "";
??? var nowd=new Date();
??? var now = nowd.getTime();
??? var delay = now - time;
??? if (delay > (10 * 24 * 60 * 60 * 1000)) {
??? ??? nowd.setTime(time);
??? ??? ret = nowd.toLocaleString();
??? } else if (delay >= (24 * 60 * 60 * 1000)) {
??? ??? delay = (delay / (24 * 60 * 60 * 1000));
??? ??? var num = Math.floor(delay);
??? ??? if (num == 1) {
??? ??? ??? ret = "昨天";
??? ??? } else if (num == 2) {
??? ??? ??? ret = "前天";
??? ??? } else {
??? ??? ??? ret = num + "天前";
??? ??? }
??? } else if (delay >= (60 * 60 * 1000)) {
??? ??? delay=(delay / (60 * 60 * 1000))
??? ??? ret = Math.floor(delay) + "小时前";
??? } else if (delay >= (60 * 1000)) {
??? ??? delay=(delay / (60 * 1000))
??? ??? ret = Math.floor(delay) + "分钟前";
??? } else if (delay >= (1000)) {
??? ??? delay=(delay / (1000))
??? ??? ret = Math.floor(delay) + "秒前";
??? } else {
??? ??? ret = "刚刚";
??? }
??? return ret;
}