Js time comparison sample share of date comparison
- 2020-03-30 02:16:31
- OfStack
<html>
<head>
<script language="javascript" type="text/javascript">
function compareDate(strDate1,strDate2)
{
var date1 = new Date(strDate1.replace(/-/g, "/"));
var date2 = new Date(strDate2.replace(/-/g, "/"));
return date1-date2;
}
function doCompare(){
var strDate1 = document.getElementById("strDate1").value;
var strDate2 = document.getElementById("strDate2").value;
var result = compareDate(strDate1,strDate2);
if ( result>0 ) {
alert("strDate1 Later than strDate2");
}else if( result<0 ){
alert("strDate1 As early as strDate2");
}else if ( result==0 ){
alert("strDate1 Is equal to the strDate2");
}
}
</script>
</head>
<body>
<input type="text" id="strDate1" name="strDate1" value="2012-07-01"/>
<input type="text" id="strDate2" name="strDate2" value="2012-08-01"/>
<input type="button" id="compareBtn" name="compareBtn" value=" To compare " onClick="doCompare();"/>
</body>
</html>