SoFunction
Updated on 2025-03-01

JS simple time comparison method

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 = ("-");
  var startTime = new Date(arrStart[0], arrStart[1], arrStart[2]);
  var startTimes = ();
  var arrEnd = ("-");
  var endTime = new Date(arrEnd[0], arrEnd[1], arrEnd[2]);
  var endTimes = ();
  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 = (0, 10).split('-');
  var endTimes = (0, 10).split('-');
  startTime = startTimes[1] + '-' + startTimes[2] + '-' + startTimes[0] + ' ' + (10, 19);
  endTime = endTimes[1] + '-' + endTimes[2] + '-' + endTimes[0] + ' ' + (10, 19);
  var thisResult = ((endTime) - (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 'abnormal';
  }
}

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./code/unixtime

Online time inquiry around the world:
http://tools./zhuanhuanqi/worldtime

Online Perpetual Calendar:
http://tools./bianmin/wannianli

Web Perpetual Calendar:
http://tools./bianmin/webwannianli

For more information about JavaScript, please view the special topic of this site: "Summary of JavaScript time and date operation skills》、《Summary of JavaScript switching effects and techniques》、《Summary of JavaScript search algorithm skills》、《Summary of JavaScript animation special effects and techniques》、《Summary of JavaScript Errors and Debugging Skills》、《Summary of JavaScript data structure and algorithm techniques》、《JavaScript traversal algorithm and skills summary"and"Summary of JavaScript mathematical operations usage

I hope this article will be helpful to everyone's JavaScript programming.