javascript時間差外掛程式分享_javascript技巧

來源:互聯網
上載者:User

javascript時間差外掛程式分享,供大家參考,具體內容如下

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>該外掛程式發布時間:<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代碼如下:

/** * 函數使用說明: *   1、直接調用函數 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("開始日期大於結束日期,計算失敗!");    return 0;  }  /**   * 通過最開始強調的各個時間段用毫秒錶示的數值,進行時間上的取整,為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;}

 結果如圖:

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.