- <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晚于strDate2");
- }else if( result<0 ){
- alert("strDate1早于strDate2");
- }else if ( result==0 ){
- alert("strDate1等于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="比较" onClick="doCompare();"/>
- </body>
- </html>
- //该片段来自于http://www.codesnippet.cn/detail/270620134324.html
来源: http://www.codesnippet.cn/detail/270620134324.html