javascript時(shí)間差插件分享
javascript時(shí)間差插件分享,供大家參考,具體內(nèi)容如下
Html如下:
<html> <head> <title></title> <script src="js/TimeDifference.js" type="text/javascript"></script> <script src="js/jquery-1.10.2-min.js" type="text/javascript"></script> </head> <body> <h2>該插件發(fā)布時(shí)間:<small id="allDemo"></small> </h2> <script type="text/javascript"> $("#allDemo").text(timeDifference("2016-06-05 10:11:00")); </script> <font color="red" id="demo1Font">2016-06-03 10:20:23 </font><br> 距離目前時(shí)間差: <strong><font color="red"><span id="timeDifferenceDemo1"></span></font></strong><br> <font color="red" id="demo2Font">2016-06-07 10:02:23 </font><br> 距離目前時(shí)間差: <strong><font color="red"><span id="timeDifferenceDemo2"></span></font></strong> </body> <script type="text/javascript"> $(document).ready(function(){ //2016-5-3 10:20:23 var demo1Result=timeDifference($("#demo1Font").text()); $("#timeDifferenceDemo1").text(demo1Result); $("#timeDifferenceDemo2").text(timeDifference($("#demo2Font").text())); }); </script> </html>
TimeDifference.js代碼如下:
/** * 函數(shù)使用說(shuō)明: * 1、直接調(diào)用函數(shù) TimeDifference() * 返回說(shuō)明: 返回距離當(dāng)前的時(shí)間差 * */ function timeDifference(tmpTime) { var mm=1000;//1000毫秒 代表1秒 var minute = mm * 60; var hour = minute * 60; var day = hour * 24; var month = day * 30; var ansTimeDifference=0;//記錄時(shí)間差 var tmpTimeStamp = tmpTime ? Date.parse(tmpTime.replace(/-/gi, "/")) : new Date().getTime();//將 yyyy-mm-dd H:m:s 進(jìn)行正則匹配 var nowTime = new Date().getTime();//獲取當(dāng)前時(shí)間戳 var tmpTimeDifference = nowTime - tmpTimeStamp;//計(jì)算當(dāng)前與需要計(jì)算的時(shí)間的時(shí)間戳的差值 if (tmpTimeDifference < 0) { //時(shí)間超出,不能計(jì)算 alert("開始日期大于結(jié)束日期,計(jì)算失??!"); return 0; } /** * 通過(guò)最開始強(qiáng)調(diào)的各個(gè)時(shí)間段用毫秒表示的數(shù)值,進(jìn)行時(shí)間上的取整,為0的話,則沒有到達(dá) * */ var DifferebceMonth = tmpTimeDifference / month; //進(jìn)行月份取整 var DifferebceWeek = tmpTimeDifference / (7 * day);//進(jìn)行周取整 var DifferebceDay = tmpTimeDifference / day;//進(jìn)行天取整 var DifferebceHour = tmpTimeDifference / hour;//進(jìn)行小時(shí)取整 var DifferebceMinute = tmpTimeDifference / minute;//進(jìn)行分鐘取整 if (DifferebceMonth >= 1) { return tmpTime; //大于一個(gè)月 直接返回時(shí)間 } else if (DifferebceWeek >= 1) { ansTimeDifference= parseInt(DifferebceWeek) + "個(gè)星期前"; } else if (DifferebceDay >= 1) { ansTimeDifference = parseInt(DifferebceDay) + "天前"; } else if (DifferebceHour >= 1) { ansTimeDifference = parseInt(DifferebceHour) + "個(gè)小時(shí)前"; } else if (DifferebceMinute >= 1) { ansTimeDifference = parseInt(DifferebceMinute) + "分鐘前"; } else { ansTimeDifference = "剛剛"; } return ansTimeDifference; }
結(jié)果如圖:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
uniapp如何實(shí)現(xiàn)tabBar之間傳參
這篇文章主要給大家介紹了關(guān)于uniapp如何實(shí)現(xiàn)tabBar之間傳參的相關(guān)資料,文中通過(guò)代碼示例介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-08-08微信小程序封裝的HTTP請(qǐng)求示例【附升級(jí)版】
這篇文章主要介紹了微信小程序封裝的HTTP請(qǐng)求,結(jié)合實(shí)例形式分析了微信小程序封裝基于wx.request方法的http請(qǐng)求相關(guān)操作與使用技巧,并附帶升級(jí)版示例供大家參考,需要的朋友可以參考下2019-05-05動(dòng)態(tài)加載圖片路徑 保持JavaScript控件的相對(duì)獨(dú)立性
根據(jù)新界面的要求,需要一部分圖片來(lái)增強(qiáng)日期控件的美觀性。考慮到既要實(shí)現(xiàn)加載圖表的目標(biāo),又要保持控件的獨(dú)立性以便將來(lái)的移植。2010-09-09微信小程序?qū)崿F(xiàn)點(diǎn)擊圖片旋轉(zhuǎn)180度并且彈出下拉列表
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)點(diǎn)擊圖片旋轉(zhuǎn)180度并且彈出下拉列表,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11javascript獲取select的當(dāng)前值示例代碼(兼容IE/Firefox/Opera/Chrome)
本篇文章主要介紹了javascript獲取select的當(dāng)前值示例代碼(兼容IE/Firefox/Opera/Chrome) 需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-12-12