날짜

2011-07-26 2 views
-1
을 비교하기 위해 자바 스크립트를 필요

가능한 중복 :날짜

을 다음과 같이
Compare 2 dates with JavaScript

내가 2 날짜를 비교하는 자바 스크립트 기능을하고 싶은, 기능이 있어야한다 If i given a date as 1-1-2011 and another as 31-12-2011 or 1-1-2011 it should prompt me a message such that date should be greater than the previous one.

어느 한 날

+3

당신이 적어도 검색한다! http://stackoverflow.com/questions/492994/compare-2-dates-with-javascript –

+1

당신은 무엇을 시도 했습니까? 어떤 오류가 발생합니까? 당신의 구체적인 문제는 무엇입니까? * (여기에있는 많은 사람들이 도움을 줄 수 있지만 코드를 제공하지는 않을 것입니다 : 몇 가지 노력을 보여줘야 함) * –

답변

0

나는 당신이 당신의 필요에 따라 시도 샘플 줄 것이다 도울 수

<script type="text/javascript"> 
    function CompareDates() { 
     var str1 = document.getElementById('<%= txtDate.ClientID %>').value; 
     var str2 = document.getElementById('<%= txtDate1.ClientID %>').value; 
     var dt1 = parseInt(str1.substring(0, 2), 10); 
     var mon1 = parseInt(str1.substring(3, 5), 10); 
     var yr1 = parseInt(str1.substring(6, 10), 10); 
     var dt2 = parseInt(str2.substring(0, 2), 10); 
     var mon2 = parseInt(str2.substring(3, 5), 10); 
     var yr2 = parseInt(str2.substring(6, 10), 10); 
     var date1 = new Date(yr1, mon1, dt1); 
     var date2 = new Date(yr2, mon2, dt2); 
     if (date2 < date1) { 
      alert("To date cannot be greater than from date"); 
      return false; 
     } 
    } 
</script> 

<asp:TextBox ID="txtDate" runat="server"></asp:TextBox> 
     <asp:TextBox ID="txtDate1" runat="server" onBlur="CompareDates();"></asp:TextBox>