javascript时间差插件分享
2016-07-18 11:15
656 查看
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; }
结果如图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
相关文章推荐
- Extjs4.0 最新最全视频教程
- Javascript中toFixed方法的改进
- 5个常见可用性错误和解决方案
- js数组实现图片轮播
- js可突破windows弹退效果代码
- JSP脚本漏洞面面观
- 使用BAT一句话命令实现快速合并JS、CSS
- js显示当前星期的起止日期的脚本
- PowerShell中计算时间差的方法
- 爆炸式的JS圆形浮动菜单特效代码
- js select常用操作控制代码
- JS实现不使用图片仿Windows右键菜单效果代码
- 从jsp发送动态图像
- 原生js结合html5制作小飞龙的简易跳球
- js 页面模块自由拖动实例
- js实现小鱼吐泡泡在页面游动特效
- js 提交和设置表单的值
- PHP VBS JS 函数 对照表
- node.js抓取并分析网页内容有无特殊内容的js文件
- PHP+JS实现大规模数据提交的方法