1

我有一个textbox从用户那里获取输入日期的。现在我想做一个validator检查日期是否大于今天。

我试过这个链接,但它有一些问题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>

**

请帮我解决这个问题

4

5 回答 5

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");
                }
于 2013-03-18T11:40:13.973 回答
2

好的,我已经做到了

CompareValidator1.ValueToCompare = DateTime.Today.ToString("MM/dd/yyyy");
于 2013-03-18T10:51:35.300 回答
1

问题是25/3/2013明确25th March 2013的,但是如果文化设置错误,01/04/13可能4th january 2013确实在今天之前。我假设您认为您正在进入1st April 2013之后。

解决方案是其中之一

  • 在文本框中输入时使用明确的日期格式(2013-01-044 月 1 日)
  • 使用显示实际日期的日期选择器组件
  • 以您期望的方式解析日期 ( dd/MM/yyyy)

问题asp:CompareValidator在于它似乎不理解日期可以采用不同的格式,并且只使用 a 的ToShortDateString变体DateTime进行比较(无论谁实现了这个都应该被枪杀!)。根据这个问题的解决方案似乎是使用CustomValidator

protected void DateTimeComparision_ServerValidate(object source, ServerValidateEventArgs args)
{
    args.IsValid = DateTime.ParseExact(txtDate.Text,"dd/MM/yyyy") > DateTime.Today 
}
于 2013-03-18T10:29:24.080 回答
0

它认为 2013 年 1 月 4 日是 1 月 4 日。您应该使用新的 DateTime(Year, Month, Day) 构造函数创建一个 DateTime 对象,并且比较将正常工作,即

var compareDate = new DateTime(2013,4,1)
bool afterToday = DateTime.Today < compareDate
于 2013-03-18T10:28:34.400 回答
0

这种日期验证应该在客户端进行。在我的应用程序中,我们使用了以下代码

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;
    },

现在像这样调用上述函数 (isFutureDate(convert("your form date value")))。

于 2013-03-18T10:32:58.743 回答