效果圖:

代碼如下:
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <ul> <li class="one-comment"> <a class="c-user-photo" href=""><img src=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" img/user_photo.png" alt="" /></a> <div class="comment-info"> <h3 class="userinfo clearfix"><a href="" class=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name">大黃蜂</a><span class="fl-right tags"><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" class="f-reply">回復(fù)</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" class="f-support">點(diǎn)贊</a></span></h3> <p class="time">2017-04-25 09:26:02</p> <p class="content">的確很漂亮啊,拍照技術(shù)越來越好了</p> </div> </li><li class="one-comment"> <a class="c-user-photo" href=""><img src=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" img/user_photo.png" alt="" /></a> <div class="comment-info"> <h3 class="userinfo clearfix"><a href="" class=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name">大黃蜂</a><span class="fl-right tags"><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" class="f-reply">回復(fù)</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" class="f-support">點(diǎn)贊</a></span></h3> <p class="time">2017-04-22 09:26:02</p> <p class="content">的確很漂亮啊,拍照技術(shù)越來越好了</p> </div> </li> </li><li class="one-comment"> <a class="c-user-photo" href=""><img src=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" img/user_photo.png" alt="" /></a> <div class="comment-info"> <h3 class="userinfo clearfix"><a href="" class=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name">大黃蜂</a><span class="fl-right tags"><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" class="f-reply">回復(fù)</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" class="f-support">點(diǎn)贊</a></span></h3> <p class="time">2017-04-26 17:50:02</p> <p class="content">的確很漂亮啊,拍照技術(shù)越來越好了</p> </div> </li> </li><li class="one-comment"> <a class="c-user-photo" href=""><img src=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" img/user_photo.png" alt="" /></a> <div class="comment-info"> <h3 class="userinfo clearfix"><a href="" class=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" name">大黃蜂</a><span class="fl-right tags"><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" class="f-reply">回復(fù)</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" class="f-support">點(diǎn)贊</a></span></h3> <p class="time">2017-04-26 09:26:02</p> <p class="content">的確很漂亮啊,拍照技術(shù)越來越好了</p> </div> </li> </ul> <script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script> <script type="text/javascript"> //開發(fā)中我們需要計(jì)算一條動(dòng)態(tài)的發(fā)布時(shí)間,一般我們得到的是一條日期字符串,我們需要計(jì)算當(dāng)前時(shí)間和動(dòng)態(tài)的發(fā)布時(shí)間,計(jì)算它們的差值來判斷發(fā)布在什么時(shí)候 //Date.parse() 解析一個(gè)日期時(shí)間字符串,并返回1970/1/1 午夜距離該日期時(shí)間的毫秒數(shù) //var nDays = Math.abs(parseInt((time2 - time1)/1000/3600/24)); // 獲取當(dāng)前時(shí)間戳(以s為單位) function setTime(time,timeSelector){ var currentTime = Date.parse(new Date()); var dateTime = time;//后臺傳遞來的時(shí)間 var ts = timeSelector;//選擇器 var d_day = Date.parse(new Date(dateTime)); var day = Math.abs(parseInt((d_day - currentTime)/1000/3600/24));//計(jì)算日期 var hour = Math.abs(parseInt((d_day - currentTime)/1000/3600));//計(jì)算小時(shí) var minutes = Math.abs(parseInt((d_day - currentTime)/1000/60));//計(jì)算分鐘 var seconds = Math.abs(parseInt((d_day - currentTime)/1000));//計(jì)算秒 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)+"小時(shí)前").toString(); }else if(minutes > 0 && minutes < 60){ ts.text(parseInt(minutes)+"分鐘前").toString(); }else if(seconds > 0 && seconds < 60){ ts.text(parseInt(seconds)+"秒前").toString(); } } //列表里面調(diào)用方法,傳遞兩個(gè)參數(shù) 數(shù)據(jù)庫時(shí)間和選擇器 $(".one-comment").each(function(){ var t_time = $(this).find(".time").text(); var timeSelector = $(this).find(".time"); setTime(t_time,timeSelector); }) </script> </body></html>以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持武林網(wǎng)!
新聞熱點(diǎn)
疑難解答