2013-03-18 124 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"); 
       }