javascript時間差插件分享
更新時間:2016年07月18日 11:15:19 投稿:lijiao
這篇文章主要為大家分享了javascript時間差插件,具有一定的參考價值,感興趣的小伙伴們可以參考一下
javascript時間差插件分享,供大家參考,具體內(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ā)布時間:<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>
距離目前時間差:
<strong><font color="red"><span id="timeDifferenceDemo1"></span></font></strong><br>
<font color="red" id="demo2Font">2016-06-07 10:02:23 </font><br>
距離目前時間差:
<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ù)使用說明:
* 1、直接調(diào)用函數(shù) TimeDifference()
* 返回說明: 返回距離當前的時間差
* */
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;//記錄時間差
var tmpTimeStamp = tmpTime ? Date.parse(tmpTime.replace(/-/gi, "/")) : new Date().getTime();//將 yyyy-mm-dd H:m:s 進行正則匹配
var nowTime = new Date().getTime();//獲取當前時間戳
var tmpTimeDifference = nowTime - tmpTimeStamp;//計算當前與需要計算的時間的時間戳的差值
if (tmpTimeDifference < 0) { //時間超出,不能計算
alert("開始日期大于結(jié)束日期,計算失??!");
return 0;
}
/**
* 通過最開始強調(diào)的各個時間段用毫秒表示的數(shù)值,進行時間上的取整,為0的話,則沒有到達
* */
var DifferebceMonth = tmpTimeDifference / month; //進行月份取整
var DifferebceWeek = tmpTimeDifference / (7 * day);//進行周取整
var DifferebceDay = tmpTimeDifference / day;//進行天取整
var DifferebceHour = tmpTimeDifference / hour;//進行小時取整
var DifferebceMinute = tmpTimeDifference / minute;//進行分鐘取整
if (DifferebceMonth >= 1) {
return tmpTime; //大于一個月 直接返回時間
} else if (DifferebceWeek >= 1) {
ansTimeDifference= parseInt(DifferebceWeek) + "個星期前";
} else if (DifferebceDay >= 1) {
ansTimeDifference = parseInt(DifferebceDay) + "天前";
} else if (DifferebceHour >= 1) {
ansTimeDifference = parseInt(DifferebceHour) + "個小時前";
} else if (DifferebceMinute >= 1) {
ansTimeDifference = parseInt(DifferebceMinute) + "分鐘前";
} else {
ansTimeDifference = "剛剛";
}
return ansTimeDifference;
}
結(jié)果如圖:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
最好用的二級聯(lián)動 原生js實現(xiàn)你值得擁有
二級聯(lián)動效果,實現(xiàn)方法有很多,不過其他文章中介紹的都比較籠統(tǒng),在本文有一個詳細的實現(xiàn)過程,使用原生js很容易理解,希望大家可以參考下2013-09-09
JavaScript實現(xiàn)復制粘貼剪切功能三種方法
這篇文章主要給大家介紹了關(guān)于JavaScript實現(xiàn)復制粘貼剪切功能的相關(guān)資料,在實際案例的操作過程中,不少人都會遇到這樣的開發(fā)需求,文中通過代碼將三種方法介紹的非常詳細,需要的朋友可以參考下2024-01-01
bootstrapfileinput實現(xiàn)文件自動上傳
這篇文章主要介紹了bootstrapfileinput實現(xiàn)文件自動上傳,bootstrap fileinput插件對多種類型的文件提供文件預覽,并且提供了多選等功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11

