【问题标题】:Compare date with today in asp.net在 asp.net 中比较日期和今天
【发布时间】:2013-03-18 10:25:40
【问题描述】:

我有一个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>

**

请帮我解决这个问题

【问题讨论】:

  • 你的比较码是什么?我希望你不是想比较字符串? (您仍然可以比较它,但需要不同的表示)
  • 你必须检查当前的文化:它可能需要01/04/2013 作为4 Jan 2013 而不是1 April 2013
  • 给我们看一些代码!您确实将类型设置为Date,不是吗?
  • 你能告诉我们你的代码吗?从你的问题中无法说出你做了什么。
  • @DawoodAbbasi 看起来像典型的文化操纵问题。尝试制定当前的文化/用户界面文化。尝试在会话开始时强制培养,看看这是否会改变任何行为。

标签: c# asp.net validation datetime


【解决方案1】:

使用下面的代码比较指定日期和今天的日期

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");
                }

【讨论】:

    【解决方案2】:

    好的,我已经这样做了

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

    【讨论】:

      【解决方案3】:

      问题是25/3/2013 是明确的25th March 2013,但是如果文化设置错误,01/04/13 可能是4th january 2013,这确实早于今天的日期。我猜你以为你输入的是1st April 2013,这将在之后。

      解决方案之一

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

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

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

      【讨论】:

      • 我以 01/04/2013 的形式输入,实际上是 2013 年 4 月 1 日,我的文化是“en-GB”
      【解决方案4】:

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

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

      【讨论】:

        【解决方案5】:

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

        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")))。

        【讨论】:

        • 如果你正在执行客户端验证,那很好,但你永远不应该依赖它——也可以实现服务器验证。
        • 是的,如果用户在他的浏览器中明确关闭了启用 javascript 选项,那么这段代码就会一头雾水..这只是对他的问题的快速解决而已.. :)
        猜你喜欢
        • 1970-01-01
        • 2011-01-08
        • 1970-01-01
        • 2023-03-13
        • 2019-03-03
        • 2014-09-09
        • 1970-01-01
        • 2020-01-04
        相关资源
        最近更新 更多