This article describes the JavaScript date comparison method. Share it for your reference, as follows:
In order to realize such a function today - users can manually enter dates, but the date can only be entered before today, and the time after today cannot be submitted, that is, today is the 15th day, and only the date before 15th day, and the date after 15th cannot be entered.
/** Convert a string in the yyyyMMdd format to the date type date is the date string */function getDate(date){ var year = date.substr(0,4);//substr() is calculated from 0 var month = date.substr(4,2);//month is 0 to 11 months var day = date.substr(6,2); alert(year+"-"+month+"-"+day); return new Date(year,month,day);}var date = getDate("20120704"); var date2 = getDate("20120720");//alert(date.getTime());//alert(date2.getTime());alert(date2.getTime() - date.getTime())The second method (including time, minute and second)
var d1=new Date("2004/09/16 20:08:00");var d2=new Date("2004/09/16 10:18:03");//What we get is the time difference between the two var d3=d1-d2;//alert(d1.getTime());//alert(d2.getTime());//The information popped up below is the same alert(d3);alert(d1.getTime()-d2.getTime());The third method (can not include time, minute and second)
var d1=new Date("2004/09/17"); var d2=new Date("2004/09/16");//What we get is the time difference between the two var d3=d1-d2;//alert(d1.getTime());//alert(d2.getTime());//The result of the pop-up message below is the same alert(d3); alert(d1.getTime()-d2.getTime());PS: Here is another online timestamp conversion tool, which contains table descriptions of timestamp operation methods for various commonly used programming languages such as javascript, php, java, Python, C#, etc. I believe that in your future programming development, you will use it:
Unix timestamp conversion tool:
http://tools.VeVB.COM/code/unixtime
For more information about JavaScript related content, please check out the topics of this site: "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.