用JS实现字符串转日期并比较大小的方法

用JS实现字符串转日期并比较大小的方法 View Code
 1 方法1: function compareDate(DateOne,DateTwo) {

 2 

 3 var OneMonth = DateOne.substring(5,DateOne.lastIndexOf ("-")); var OneDay = DateOne.substring(DateOne.length,DateOne.lastIndexOf ("-")+1); var OneYear = DateOne.substring(0,DateOne.indexOf ("-"));

 4 

 5 var TwoMonth = DateTwo.substring(5,DateTwo.lastIndexOf ("-")); var TwoDay = DateTwo.substring(DateTwo.length,DateTwo.lastIndexOf ("-")+1); var TwoYear = DateTwo.substring(0,DateTwo.indexOf ("-"));

 6 

 7 if (Date.parse(OneMonth+"/"+OneDay+"/"+OneYear) > Date.parse(TwoMonth+"/"+TwoDay+"/"+TwoYear)) { return true; } else { return false; }

 8 

 9 }

10 

11  

12 

13 举例: alert(compareDate(''2004-12-01'',''2004-05-02''')); 目前只支持年-月-日这样的格式

14 

15  

16 

17 方法2:

18 

19 var checkStartDate = document.form1.checkStartDate.value; 

20 

21 var checkEndDate = document.form1.checkEndDate.value; 

22 

23 var arys= new Array();  

24 

25 var startdate=new Date(arys[0],parseInt(arys[1]-1),arys[2]);  

26 

27 if(checkStartDate != null && checkEndDate != null) {  

28 

29     arys=checkStartDate.split('-');  

30 

31     var startdate=new Date(arys[0],parseInt(arys[1]-1),arys[2]); 

32 

33     arys=checkEndDate.split('-');  

34 

35     var checkEndDate=new Date(arys[0],parseInt(arys[1]-1),arys[2]);   

36 

37     if(startdate > checkEndDate) {  

38 

39         alert("你的检查日期开始时间大于结束时间啦");  

40 

41         return; 

42 

43     }

44 

45 } 

本文转载于:http://blog.163.com/wzi_xiang/blog/static/659829612009111731536436/

你可能感兴趣的:(字符串)