javascript时间差插件分享

所属分类: 网络编程 / JavaScript 阅读数: 556
收藏 0 赞 0 分享

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;
}

 结果如图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Angular使用Md5加密的解决方法

这篇文章主要介绍了Angular使用Md5加密的解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

详解JS构造函数中this和return

本文通过实例代码给大家介绍了JS构造函数中this和return,需要的朋友参考下吧
收藏 0 赞 0 分享

ES6中Array.find()和findIndex()函数的用法详解

ES6为Array增加了find(),findIndex函数。find()函数用来查找目标元素,找到就返回该元素,找不到返回undefined,而findIndex()函数也是查找目标元素,找到就返回元素的位置,找不到就返回-1。下面通过实例详解,需要的朋友参考下吧
收藏 0 赞 0 分享

JS闭包的几种常见形式实例详解

本文通过实例代码给大家详细介绍了js闭包的几种常见形式,代码简单易懂,非常不错,具有参考借鉴价值,需要的朋友参考下
收藏 0 赞 0 分享

ES6中Array.copyWithin()函数的用法实例详解

ES6为Array增加了copyWithin函数,用于操作当前数组自身,用来把某些个位置的元素复制并覆盖到其他位置上去。下面重点给大家介绍ES6中Array.copyWithin()函数的用法,需要的朋友参考下
收藏 0 赞 0 分享

Javascript 严格模式use strict详解

严格模式:由ECMA-262规范定义的JavaScript标准,对javascrip的限制更强。这篇文章主要介绍了Javascript 严格模式use strict详解 ,需要的朋友可以参考下
收藏 0 赞 0 分享

引入JavaScript时alert弹出框显示中文乱码问题

今天在HTML中引入JavaScript文件运行时,alert弹出的提示框中文显示为乱码,怎么解决此问题呢?下面小编给大家带来了引入JavaScript时alert弹出框显示中文乱码问题的解决方法,一起看看吧
收藏 0 赞 0 分享

AngularJs 延时器、计时器实例代码

这篇文章主要介绍了AngularJs 延时器、计时器实例代码,需要的朋友可以参考下
收藏 0 赞 0 分享

JS分页的实现(同步与异步)

这篇文章主要介绍了JS分页的实现(同步与异步),需要的朋友可以参考下
收藏 0 赞 0 分享

Angularjs自定义指令实现分页插件(DEMO)

由于最近的一个项目使用的是angularjs1.0的版本,涉及到分页查询数据的功能,后来自己就用自定义指令实现了该功能,下面小编把实例demo分享到脚本之家平台,需要的朋友参考下
收藏 0 赞 0 分享
查看更多