This time I will bring you js for time size comparison. What are the precautions for time size comparison with js? The following is a practical case, let’s take a look.
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>
The above is the detailed content of js for time size comparison. For more information, please follow other related articles on the PHP Chinese website!