Js time comparison example
This article mainly introduces the js time comparison example. The code is simple and you can see the result after running it. For more information, see
The Code is as follows:
<Html>
<Head>
<Script language = "javascript" type = "text/javascript">
/** Date comparison **/
Function compareDate (strDate1, strDate2)
{
Var date1 = new Date (strDate1.replace (/-/g ,"/"));
Var date2 = new Date (strDate2.replace (/-/g ,"/"));
Return date1-date2;
}
/** Compare **/
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 earlier than strDate2 ");
} Else if (result = 0 ){
Alert ("strDate1 equals 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 = "Compare" onClick = "doCompare ();"/>
</Body>
</Html>