This article describes the method of obtaining and verifying the start and end date of JS. Share it for your reference, as follows:
function validation(){var startdate=document.getElementById("start_tenancyDate_s").value;var enddate=document.getElementById("end_tenancyDate_s").value;var datesent=dateDiff(enddate,startdate);if(startdate==""){alert("Please select the start date!");return false;}if(enddate==""){alert("Please select the end date!");return false;}if(datesent>365){alert("The selected date difference exceeds the maximum value of 1 year!");return false;}if(datesent<0){alert("The selected date is incorrect, the end date must be greater than the start date!");return false;}else{return true;}}//Call this method (main method)function dateDiff(date1, date2){ var type1 = typeof date1, type2 = typeof date2; if(type1 == 'string') date1 = stringToTime(date1); else if(date1.getTime) date1 = date1.getTime(); if(type2 == 'string') date2 = stringToTime(date2); else if(date2.getTime) date2 = date2.getTime(); return (date1 - date2) / (1000 * 60 * 60 * 24);//Except 1000 is milliseconds, no second is added}//The method required to convert a stringToTime(string){ var f = string.split(' ', 2); var d = (f[0] ? f[0] : '').split('-', 3); var t = (f[1] ? f[1] : '').split(':', 3); return (new Date( parseInt(d[0], 10) || null, (parseInt(d[1], 10) || 1)-1, parseInt(d[2], 10) || null, parseInt(t[0], 10) || null, parseInt(t[1], 10) || null, parseInt(t[2], 10) || null )).getTime();}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.