Home  >  Article  >  Web Front-end  >  js only compares instances of time size

js only compares instances of time size

小云云
小云云Original
2018-01-02 16:03:001525browse

This article mainly brings you an example of js comparing only time size. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor to take a look, I hope it can help everyone.

The examples are as follows:

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

Related recommendations:

php function to compare whether there is an intersection between time periods and time periods

JavaScript code for comparing time sizes_time and date

Detailed explanation of the conversion method between js timestamp and date format

The above is the detailed content of js only compares instances of time size. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn