当前位置:Gxlcms > JavaScript > js进行时间大小比较

js进行时间大小比较

时间:2021-07-01 10:21:17 帮助过:10人阅读

这次给大家带来js进行时间大小比较,js进行时间大小比较的注意事项有哪些,下面就是实战案例,一起来看一下。

实例如下:

<script> 
 ///比较函数 
 function compareTime(date1, date2) { 
  var d1_year, d1_month, d1_day, d1, time_span1, 
    d2_year, d2_month, d2_day, d2, time_span2; 
  d1_year = date1.getFullYear(); 
  d1_month = date1.getMonth() + 1; 
  d1_day = date1.getDate(); 
  d1 = new Date(d1_year, d1_month - 1, d1_day, 0, 0, 0); 
  time_span1 = date1.getTime() - d1.getTime(); 
  d2_year = date2.getFullYear(); 
  d2_month = date2.getMonth() + 1; 
  d2_day = date2.getDate(); 
  d2 = new Date(d2_year, d2_month - 1, d2_day, 0, 0, 0); 
  time_span2 = date2.getTime() - d2.getTime(); 
  return time_span1 - time_span2; 
 } 
 //测试 
 <SPAN style="COLOR: #99cc00"> var date1 = new Date(2017, 10, 21, 20, 20, 21); 
 var date2 = new Date(2016, 5, 2, 20, 20, 30); 
 alert(compareTime(date1, date2));</SPAN> 
</script>

相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!

推荐阅读:



以上就是js进行时间大小比较的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行