2013-03-18 109 views
1

我有一個textbox,它從用戶處獲取輸入日期。現在我想做一個validator,它檢查任何一天的日期是否大於今天。與asp.net中的今日比較日期

我試過這個鏈接,但它也存在一些問題http://forums.asp.net/t/1116715.aspx/1

如果我給這個日期25/03/2013它是正確的,但如果給01/04/2013,它說,這是不到今天。

**

更新

<asp:CompareValidator ID="CompareValidator2" runat="server" ControlToValidate="txtReturnDate" 
           Display="Dynamic" ErrorMessage="Date should be greater then today" ForeColor="Red" 
           Operator="GreaterThan" ValidationGroup="VI">Date should be greater then today</asp:CompareValidator> 

**

請幫我解決這個問題

+0

什麼是你比較代碼比較指定的日期?我希望你沒有試圖比較字符串? (你仍然可以比較它,但它將需要不同的代表) – Yahya 2013-03-18 10:28:55

+1

你必須檢查當前的文化:它可能需要'01/04/2013'作爲'2013年1月4日'而不是'2013年4月1日' – Sachin 2013-03-18 10:29:00

+0

告訴我們一些代碼!您確實將類型設置爲「日期」,是不是? – 2013-03-18 10:29:00

回答

2

確定我已經通過下面的代碼

CompareValidator1.ValueToCompare = DateTime.Today.ToString("MM/dd/yyyy"); 
0

它認爲2013年1月4日是1月4日。你應該使用新的日期時間(年,月,日)構造函數的comparisson將正常工作,即

var compareDate = new DateTime(2013,4,1) 
bool afterToday = DateTime.Today < compareDate 
1

的問題是,25/3/2013是unambiguosly 25th March 2013創建一個DateTime對象,但是用錯誤的文化設置,01/04/13可能是4th january 2013這確實是在今天的日期之前。我假設你認爲你正在輸入1st April 2013這將是後。

的解決方案是

  • 使用一個明確的日期格式的鍵入到您的文本框(2013-01-04爲4月1日)
  • 使用日期選擇器組件,它暴露了實際日期
  • 解析日期時(dd/MM/yyyy

asp:CompareValidator的問題是,它似乎沒有低估並且這些日期的格式可能不同,並且只使用DateTimeToShortDateString變體進行比較(實施此操作的人應該被拍攝!)。根據this question的解決方案似乎是使用CustomValidator

protected void DateTimeComparision_ServerValidate(object source, ServerValidateEventArgs args) 
{ 
    args.IsValid = DateTime.ParseExact(txtDate.Text,"dd/MM/yyyy") > DateTime.Today 
} 
+1

我以2013年4月1日的形式接受輸入,實際上是2013年4月1日,我的文化是「en-GB」 – 2013-03-18 10:40:29

0

該類日期驗證的應side..in我的應用程序,我們使用了下面的代碼

convert: function (d) { 
     /* Converts the date in d to a date-object. The input can be: 
     a date object: returned without modification 
     an array  : Interpreted as [year,month,day]. NOTE: month is 0-11. 
     a number  : Interpreted as number of milliseconds 
     since 1 Jan 1970 (a timestamp) 
     a string  : Any format supported by the javascript engine, like 
     "YYYY/MM/DD", "MM/DD/YYYY", "Jan 31 2009" etc. 
     an object  : Interpreted as an object with year, month and date 
     attributes. **NOTE** month is 0-11. */ 
     return (
     d.constructor === Date ? d : 
     d.constructor === Array ? new Date(d[0], d[1], d[2]) : 
     d.constructor === Number ? new Date(d) : 
     d.constructor === String ? new Date(d) : 
     typeof d === "object" ? new Date(d.year, d.month, d.date) : 
     NaN 
    ); 

isFutureDate: function (a) { 
     var now = new Date(); 
     return (a > now) ? true : false; 
    }, 

現在在客戶端上permormed調用上述函數(isFutureDate(convert(「form date value」)))。

+0

如果您正在進行客戶端驗證,那很好,但您永遠不應該依賴它 - 實現服務器驗證。 – Jamiec 2013-03-18 10:34:55

+0

燁,如果用戶明確關閉在他的瀏覽器中啓用JavaScript選項,然後這段代碼落在它的臉上..這只是一個快速修復他的問題,這就是所有.. :) – 2013-03-18 10:41:33

3

使用做到了這一點與今天的日期

string date = "01/04/2013"; 
       DateTime myDate = DateTime.ParseExact(date, "dd/MM/yyyy", 
              System.Globalization.CultureInfo.InvariantCulture); 
       if (myDate > DateTime.Today) 
       { 
        Console.WriteLine("greater than"); 
       } 
       else 
       { 
       Console.WriteLine("Less Than"); 
       }