JS实现字符串转日期并比较大小实例分析

function compareDate(DateOne, DateTwo) { var OneMonth = DateOne.substring(5, DateOne.lastIndexOf("-")); var OneDay = DateOne.substring(DateOne.length, DateOne.lastIndexOf("-") + 1); var OneYear = DateOne.substring(0, DateOne.indexOf("-")); var TwoMonth = DateTwo.substring(5, DateTwo.lastIndexOf("-")); var TwoDay = DateTwo.substring(DateTwo.length, DateTwo.lastIndexOf("-") + 1); var TwoYear = DateTwo.substring(0, DateTwo.indexOf("-")); if (Date.parse(OneMonth + "https://www.jb51.net/" + OneDay + "https://www.jb51.net/" + OneYear) > Date.parse(TwoMonth + "https://www.jb51.net/" + TwoDay + "https://www.jb51.net/" + TwoYear)) { return true; } else { return false; } }

举例:

复制代码 代码如下:

alert(compareDate(''2004-12-01'',''2004-05-02'''));

目前只支持年-月-日这样的格式

方法2:

var checkStartDate = document.form1.checkStartDate.value; var checkEndDate = document.form1.checkEndDate.value; var arys= new Array(); var startdate=new Date(arys[0],parseInt(arys[1]-1),arys[2]); if(checkStartDate != null && checkEndDate != null) { arys=checkStartDate.split('-'); var startdate=new Date(arys[0],parseInt(arys[1]-1),arys[2]); arys=checkEndDate.split('-'); var checkEndDate=new Date(arys[0],parseInt(arys[1]-1),arys[2]); if(startdate > checkEndDate) { alert("你的检查日期开始时间大于结束时间啦"); return; } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgzwwp.html