首页 > 编程语言 > 详细

JavaScript 实现发布消息后,距离当前时间的实现

时间:2017-10-06 18:56:35      阅读:267      评论:0      收藏:0      [点我收藏+]

某条消息发布后,距离当前时间多久的时间显示

 

 1 //显示发布时间的函数
 2 function pastTime(_createTime) {
 3     //var createTime = _createTime.substr(0, _createTime.lastIndexOf(" ")) //不能包含毫秒,如果有毫秒,进行截取
 4     var nowTime = new Date();
 5     var cTime = new Date(_createTime);
 6     var result = parseInt((nowTime - cTime) / 1000 / 60); //分钟数
 7     if (result < 0) {
 8         result = Math.abs(720 + result);
 9     }
10     var resultStr = result + "分钟前";
11     if (result == 0) {
12         resultStr = "刚刚发布"
13     }
14     //如需显示“月”,“年” 在此处添加if...else
15     if (result >= 10080) {
16         result = parseInt(result / 60*24*7); //
17         resultStr = result + "周前"
18     } else if (result >= 1440) {
19         result = parseInt(result / 60*24); //
20         resultStr = result + "天前"
21     } else if (result >= 60) {
22         result = parseInt(result / 60); //小时
23         resultStr = result + "小时前"
24     }
25     return resultStr;
26 }

 

JavaScript 实现发布消息后,距离当前时间的实现

原文:http://www.cnblogs.com/fengguohoudejiyi/p/7631938.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!