This article describes the simple method of time comparison of JS. Share it for your reference, as follows:
//Time comparison (yyyy-MM-dd) function compareDate(startDate, endDate) { var arrStart = startDate.split("-"); var startTime = new Date(arrStart[0], arrStart[1], arrStart[2]); var startTimes = startTime.getTime(); var arrEnd = endDate.split("-"); var endTime = new Date(arrEnd[0], arrEnd[1], arrEnd[2]); var endTimes = endTime.getTime(); if (endTimes<startTimes) { alert("The end time cannot be less than the start time"); return false; } return true;}//Time comparison (yyyy-MM-dd HH:mm:ss) function compareTime(startTime,endTime) { var startTimes = startTime.substring(0, 10).split('-'); var endTimes = endTime.substring(0, 10).split('-'); startTime = startTimes[1] + '-' + startTimes[2] + '-' + startTimes[0] + ' ' + startTime.substring(10, 19); endTime = endTimes[1] + '-' + endTimes[2] + '-' + endTimes[0] + ' ' + endTime.substring(10, 19); var thisResult = (Date.parse(endTime) - Date.parse(startTime)) / 3600 / 1000; if (thisResult < 0) { alert("endTime is less than tartTime!"); } else if (thisResult > 0) { alert("endTime is greater than tartTime!"); } else if (thisResult == 0) { alert("endTime equals tartTime!"); } else { return 'Exception'; }}PS: Friends who are interested in JavaScript time and date operations can also refer to the online tools of this site:
Unix timestamp conversion tool:
http://tools.VeVB.COM/code/unixtime
Online time inquiry around the world:
http://tools.VeVB.COM/zhuanhuanqi/worldtime
Online Perpetual Calendar:
http://tools.VeVB.COM/bianmin/wannianli
Web Perpetual Calendar:
http://tools.VeVB.COM/bianmin/webwannianli
For more information about JavaScript, please check this site's special topics: "Summary of JavaScript Time and Date Operation Skills", "Summary of JavaScript Switching Effects and Skills", "Summary of JavaScript Search Algorithm Skills", "Summary of JavaScript Animation Special Effects and Skills", "Summary of JavaScript Errors and Debugging Skills", "Summary of JavaScript Data Structures and Algorithm Skills", "Summary of JavaScript Traversal Algorithm and Skills" and "Summary of JavaScript Mathematical Operation Usage"
I hope this article will be helpful to everyone's JavaScript programming.