微信小程序开发框架_js仿照微信朋友圈核算时间

js模仿微信朋友圈计算时间显示几天/几小时/几分钟/几秒之前       本篇文章主要介绍了js模仿微信朋友圈计算时间显示几天/几小时/几分钟/几秒之前的实例。具有很好的参考价值。下面跟着小编一起来看下吧

效果图:

代码如下:

 !DOCTYPE html 
 html 
 head 
 meta charset="UTF-8" 
 title /title 
 /head 
 body 
 a href="" img src=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" img/user_photo.png" alt="" / /a 
 div 
 h3 a href="" external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name" 大黄蜂 /a span a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" 回复 /a a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" 点赞 /a /span /h3 
 p :02 /p 
 p 的确很漂亮啊,拍照技术越来越好了 /p 
 /div 
 /li 
 a href="" img src=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" img/user_photo.png" alt="" / /a 
 div 
 h3 a href="" external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name" 大黄蜂 /a span a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" 回复 /a a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" 点赞 /a /span /h3 
 p :02 /p 
 p 的确很漂亮啊,拍照技术越来越好了 /p 
 /div 
 /li 
 /li 
 a href="" img src=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" img/user_photo.png" alt="" / /a 
 div 
 h3 a href="" external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name" 大黄蜂 /a span a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" 回复 /a a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" 点赞 /a /span /h3 
 p :02 /p 
 p 的确很漂亮啊,拍照技术越来越好了 /p 
 /div 
 /li 
 /li 
 a href="" img src=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" img/user_photo.png" alt="" / /a 
 div 
 h3 a href="" external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name" 大黄蜂 /a span a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" 回复 /a a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" 点赞 /a /span /h3 
 p :02 /p 
 p 的确很漂亮啊,拍照技术越来越好了 /p 
 /div 
 /li 
 /ul 
 script src="jquery/2.1.4/jquery.min.js" /script 
 script type="text/javascript" 
 //开发中我们需要计算一条动态的发布时间,一般我们得到的是一条日期字符串,我们需要计算当前时间和动态的发布时间,计算它们的差值来判断发布在什么时候
 //Date.parse() 解析一个日期时间字符串,并返回1970/1/1 午夜距离该日期时间的毫秒数
 //var nDays = Math.abs(parseInt((time2 - time1)/1000/3600/24));
 // 获取当前时间戳(以s为单位)
 function setTime(time,timeSelector){
 var currentTime = Date.parse(new Date());
 var dateTime = time;//后台传递来的时间
 var ts = timeSelector;//选择器
 var d_day = Date.parse(new Date(dateTime));
 var day = Math.abs(parseInt((d_day - currentTime)/1000/3600/24));//计算日期
 var hour = Math.abs(parseInt((d_day - currentTime)/1000/3600));//计算小时
 var minutes = Math.abs(parseInt((d_day - currentTime)/1000/60));//计算分钟
 var seconds = Math.abs(parseInt((d_day - currentTime)/1000));//计算秒
 console.log(day);
 if(day = 2){
 ts.text(parseInt(day)+"天前").toString();
 }else if(day 0 day 2){
 ts.text("昨天").toString();
 }else if(hour 0 hour 24){
 ts.text(parseInt(hour)+"小时前").toString();
 }else if(minutes 0 minutes 60){
 ts.text(parseInt(minutes)+"分钟前").toString();
 }else if(seconds 0 seconds 60){
 ts.text(parseInt(seconds)+"秒前").toString();
 //列表里面调用方法,传递两个参数 数据库时间和选择器
 $(".ment").each(function(){
 var t_time = $(this).find(".time").text();
 var timeSelector = $(this).find(".time");
 setTime(t_time,timeSelector);
 /script 
 /body 
 /html 

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持凡科!