Home > Article > Web Front-end > How to compare the number of days between two dates in javascript_javascript skills
The example in this article describes the method of comparing the number of days between two dates in javascript. Share it with everyone for your reference. The details are as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <script language="JavaScript"> function getDate(strDate){ if(strDate==null||strDate===undefined) return null; var date = new Date(); try{ if(strDate == undefined){ date= null; }else if(typeof strDate == 'string'){ strDate = strDate.replace(/:/g,'-'); strDate = strDate.replace(/ /g,'-'); var dtArr = strDate.split("-"); if(dtArr.length>=3&&dtArr.length<6){ date=new Date(dtArr[0], dtArr[1], dtArr[2]); }else if(date.length>8){ date=new Date(Date.UTC(dtArr[0],dtArr[1]-1,dtArr[2],dtArr[3]-8,dtArr[4],dtArr[5])); } }else{ date = null; } return date; }catch(e){ alert('格式化日期出现异常:' + e.message); } } function test(){ var time1 = "2011-12-12"; var time2 = "2011-12-10"; var timeslong = getDate(time1).getTime()-getDate(time2).getTime(); alert(timeslong/(1000*60*60*24)) } test(); </script> </head> <body> </body> </html>
I hope this article will be helpful to everyone’s JavaScript programming design.