您好,登錄后才能下訂單哦!
這篇文章主要介紹js如何模仿微信朋友圈計(jì)算時(shí)間顯示幾天/幾小時(shí)/幾分鐘/幾秒之前的效果,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
效果圖:
代碼如下:
<!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"> <h4 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></h4> <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"> <h4 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></h4> <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"> <h4 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></h4> <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"> <h4 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></h4> <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;//后臺(tái)傳遞來的時(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>
以上是“js如何模仿微信朋友圈計(jì)算時(shí)間顯示幾天/幾小時(shí)/幾分鐘/幾秒之前的效果”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。