-1

我有以下代码用于显示多久以前发表评论:

var timestamp = (new Date().getTime())/1000;
var comment_time = timestamp - responses[i]['time'];
var time_string = '';
if(comment_time < 60)
    time_string = Math.round(comment_time)+"s ago";
else if(comment_time < 3600)
    time_string = Math.round(comment_time/60)+"m ago";
else if(comment_time < 86400)
    time_string = Math.round(comment_time/3600)+"h ago";
else
    time_string = Math.round(comment_time/86400)+"d ago";

这工作得很好,除非评论不到一分钟。发生这种情况时,根本不会发生舍入。如果评论不到一分钟,我似乎得到了一致的 15 位有效数字。一旦超过一分钟,一切正常。关于这个还能做什么?

4

1 回答 1

1

尝试使用parseInt()带小数部分

time_string = parseInt(Math.round(comment_time/60))+"m ago";

也去那里用 jsfiddle http://jsfiddle.net/arunpjohny/6m5D8/1/

于 2013-04-28T08:05:40.210 回答