JavaScrit 判断两个时间相差的各个阶段
来源:互联网 发布:网络相关知识 编辑:程序博客网 时间:2024/06/05 20:58
<script type="text/javascript">function dateDiff( str1, str2 ) { var diff = Date.parse( str2 ) - Date.parse( str1 ); return isNaN( diff ) ? NaN : { diff : diff, ms : Math.floor( diff % 1000 ), s : Math.floor( diff / 1000 % 60 ), m : Math.floor( diff / 60000 % 60 ), h : Math.floor( diff / 3600000 % 24 ), d : Math.floor( diff / 86400000 ) }; } var obj = dateDiff(new Date(new Date("2011-03-04 12:23:00".replace("-","/")) ), new Date("2011-03-05 12:24:00".replace("-","/"))); alert( obj.ms +"毫秒" + obj.s + " 秒" + obj.m + " 分" + obj.h + " 小时" + obj.d + " 天:" );</script>
这些代码只是把两个时间做出了比较,可以自己试试,然后找出规律,做出自己想要的结果。多多分享。
以下应该更贴切些:
function mydiff(date1,date2,interval) { var second=1000, minute=second*60, hour=minute*60, day=hour*24, week=day*7; date1 = new Date(date1); date2 = new Date(date2); var timediff = date2 - date1; if (isNaN(timediff)) return NaN; switch (interval) { case "years": return date2.getFullYear() - date1.getFullYear(); case "months": return ( ( date2.getFullYear() * 12 + date2.getMonth() ) - ( date1.getFullYear() * 12 + date1.getMonth() ) ); case "weeks" : return Math.floor(timediff / week); case "days" : return Math.floor(timediff / day); case "hours" : return Math.floor(timediff / hour); case "minutes": return Math.floor(timediff / minute); case "seconds": return Math.floor(timediff / second); default: return undefined; } } var o = mydiff(new Date(new Date("2011-03-04 12:23:00".replace("-","/")) ), new Date("2011-03-05 12:24:00".replace("-","/")),"days"); alert(o);
- JavaScrit 判断两个时间相差的各个阶段
- java 判断两个时间相差的天数
- 取两个日期相差的时间
- 计算两个时间的相差天数。。
- js计算两个时间相差的天数
- 求两个时间的相差月份
- 算两个时间相差的分钟数
- 判断两个时间相差多少天多少分多少秒
- js判断两个时间是否相差9个月
- 两个时间相差天数
- 计算两个时间点间相差的时间间隔
- 计算两个时间戳之间相差的时间
- 两个时间之间 相差多少
- 计算两个时间相差天数
- 两个时间相差多少天
- 如何查看两个时间类型的变量的相差值
- javascript两个日期时间相差的秒数
- JS计算两个时间相差的分钟数
- Android AsyncTask
- Linux hostname主机名配置文件与文件 /etc/hosts解说
- FreeBSD的安装
- linux下的dns设置详解
- Spring的核心思想终极理解
- JavaScrit 判断两个时间相差的各个阶段
- Android C2DM学习——客户端代码开发
- Int/Decimal.ToString 方法 (String, IFormatProvider)
- POJ1274 The Perfect Stall 简单最大二分匹配
- eclipse3.7+resin4.0集成配置小结
- 第一个ActionScript3.0语言的文字小游戏【古惑仔之人在江湖】
- 关于contiki下rest-example的问题,和led这个resource如何跑的问题(绝对原创)
- DSP系统是硬实时系统
- BIND9 递归查询超时机制