2012-05-04 142 views
1

我對它有一個TextBox和Extender AjaxCalander。在選擇日期時,它在TextBox中的工作狀態很好,只顯示月份和年份。我的代碼如下datetime(month + year)與字符串的比較

<asp:TextBox ID="txtCardExpiry" runat="server" Width="200px" /><span> MMYY</span> 
<ajaxToolkit:CalendarExtender runat="server" Format="MMyy" TargetControlID="txtCardExpiry" /> 

我想我現在該怎麼比較txtCardExpiry.Text與今天月份和年份只有

現在即時通訊使用

int dtNowYear = Convert.ToInt16(DateTime.Now.Year.ToString().Substring(2, 2)); 
      int dtNowMonth = Convert.ToInt16(DateTime.Now.Month.ToString()); 
      int dtExpYear = Convert.ToInt16(txtCardExpiry.Text.Substring(2, 2)); 
      int dtExpMonth = Convert.ToInt16(txtCardExpiry.Text.Substring(0, 2)); 

      if (dtNowYear == dtExpYear && dtNowMonth > dtExpMonth) 
      { 
       lblMessage.Attributes.Add("class", "lblMessageError"); 
       lblMessage.Text = 
        "Credit Card expiry month and year cannot be less than todays month and year or your Credit Card has expired"; 
       return false; 
      } 
       else if (dtNowYear > dtExpYear) 
       { 
        if (dtNowMonth > dtExpMonth) 
        { 
         lblMessage.Attributes.Add("class", "lblMessageError"); 
         lblMessage.Text = 
          "Credit Card expiry month and year cannot be less than todays month and year or your Credit Card has expired"; 
         return false; 
        } 
        return false; 
       } 
+0

只是與asp.net –

回答

6
來驗證,如果信用卡未過期

嘗試

txtCardExpiry.Text.Compare(DateTime.Now.ToString("MMyyyy")) 

Custom Date and Time Format Strings 必須說,MSDN非常有幫助。

+0

我怎麼能只比較MMYY格式與datetime.now.tostring(「MMYY」) – skhurams

+2

只是'Datetime.now.tostring(「MMyy」)',** yy **是小寫。 – fankt