【问题标题】:how to compare two date using asp:CompareValidator which is in the format of dd-mm-yyyy如何使用 dd-mm-yyyy 格式的 asp:CompareValidator 比较两个日期
【发布时间】:2017-02-09 05:16:12
【问题描述】:

我正在尝试比较格式为 dd-mm-yyyy 的两个日期 我使用了下面的代码

 <input  type="text" id="ocd" size="25" title="Closure Date" placeholder="Possible Closure Date " runat="server">   // where this exists in dd-mm-yyyy format 
 <input  type="text" id="od" size="25" title="Closure Date" placeholder=" Date Of Opportunity" runat="server">    // where this exists in dd-mm-yyyy format 
 <asp:CompareValidator ID="CompareValidator1" ValidationGroup = "Date" ForeColor = "Red" runat="server" 
            ControlToValidate = "od" ControlToCompare = "ocd" Operator = "LessThanEqual" Type = "Date"
            ErrorMessage="Receipt date must be less than Closure date."></asp:CompareValidator>   

但它只比较 dd in dd-mm-yyyy 我该如何解决这个问题?

【问题讨论】:

标签: c# asp.net date datetime


【解决方案1】:
  Start Date: <asp:TextBox ID="txtStartDate" runat="server" Text = "24/02/1999"></asp:TextBox>&nbsp;
    End Date: <asp:TextBox ID="txtEndDate" runat="server" Text = "31/12/1988"></asp:TextBox><br />

<asp:CompareValidator ID="CompareValidator1" ValidationGroup = "Date" ForeColor = "Red" runat="server" 
    ControlToValidate = "txtStartDate" ControlToCompare = "txtEndDate" Operator = "LessThan" Type = "Date"
    ErrorMessage="Start date must be less than End date."></asp:CompareValidator>

下面的文章可以帮助你详细:-

http://www.aspsnippets.com/Articles/Compare-dates-in-ddmmyyyy-format-using-ASPNet-CompareValidator.aspx

编辑 1:-

我认为您需要在这里使用自定义验证器,如下面的链接所示:-

ASP.NET validator to compare two date difference is not more than 12 months

您需要在此处构建您的逻辑以进行日期比较,链接仅供参考。

编辑 2:-

protected void ValidateDuration(object sender, ServerValidateEventArgs e)
{
    DateTime firstDate= DateTime.Parse(txbStartDate.Text);
    DateTime secondDate = DateTime.Parse(txbEndDate.Text);

    if(firstDate <= secondDate)
    {
      e.IsValid = false;
    }
    else
    {
      e.IsValid = true;
    }          
}

编辑 3:-

如果上述部分不起作用,则需要更改页面文化属性的日期:-

<%@ Page Language="C#" 
    AutoEventWireup="true" 
    CodeFile="Default.aspx.cs" 
    Inherits="_Default" 
    Culture = "ar-MA" %>

有关文化的完整列表,请查看以下链接:-

http://www.basicdatepicker.com/samples/cultureinfo.aspx

【讨论】:

  • 我已经尝试过了,我的问题是日期格式 dd-mm-yyyy
  • 它实际上只比较 dd-mm-yyyy 格式的 dd
  • 抱歉错过了那部分,让我更新答案。
  • 请检查更新的答案,请根据您的需要更新Edit 2。
  • 感谢您的帮助,但它无法正常工作,它直接接受所有值并提交来自
【解决方案2】:

我已经尝试过这种方法,现在它适用于 dd-mm-yyyy 格式

在 web.config 文件中

<system.web>
<globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="en-GB" uiCulture="en-GB" /></system.web>

在 .aspx 页面中更新此内容时

添加 Culture = "en-GB"

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="filename.aspx.cs" Inherits="<%--your backend code--%>"   Culture = "en-GB"  %>

现在将 CompareValidator 添加到比较日期

 <asp:CompareValidator ID="CompareValidator1" ValidationGroup = "Date" ForeColor = "Red" runat="server" ControlToValidate = "startdate" ControlToCompare = "enddate" Operator = "LessThan" Type = "Date" ErrorMessage="Start date must be less than End date."></asp:CompareValidator>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-02
    • 1970-01-01
    • 1970-01-01
    • 2010-09-22
    • 1970-01-01
    • 2014-09-22
    相关资源
    最近更新 更多