比较两个日期的问题

发布于 2024-11-08 01:00:05 字数 2729 浏览 5 评论 0原文

我使用两个CalendarExtender来组合两个日期:开始日期结束日期,并使用比较验证器来验证结束日期晚于开始日期。

当结束日期中的日期小于开始日期中的日期时,即使整个结束日期大于整个开始日期,验证器也会触发问题。

如何解决此问题?

我的 aspx:

1-开始日期:

<asp:TextBox ID="txt_startDate" runat="server" ValidationGroup="insertgroup" MaxLength="10"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txt_startDate"
    ErrorMessage="!" ValidationGroup="insertgroup"></asp:RequiredFieldValidator>
<cc1:MaskedEditExtender ID="txt_startDate_MaskedEditExtender" runat="server" CultureAMPMPlaceholder=""
    CultureCurrencySymbolPlaceholder="" CultureDateFormat="" CultureDatePlaceholder=""
    CultureDecimalPlaceholder="" CultureThousandsPlaceholder="" CultureTimePlaceholder=""
    Enabled="True" Mask="99/99/9999" MaskType="Date" TargetControlID="txt_startDate">
</cc1:MaskedEditExtender>
<cc1:CalendarExtender ID="txt_startDate_CalendarExtender" runat="server" Enabled="True"
    TargetControlID="txt_startDate" Format="dd/MM/yyyy">
</cc1:CalendarExtender>

2-结束日期:

<asp:TextBox ID="txt_endDate" runat="server" ValidationGroup="insertgroup" MaxLength="10"></asp:TextBox>
<cc1:CalendarExtender ID="txt_endDate_CalendarExtender" runat="server" TargetControlID="txt_endDate"
    Format="dd/MM/yyyy">
</cc1:CalendarExtender>
<cc1:MaskedEditExtender ID="txt_endDate_MaskedEditExtender" runat="server" CultureAMPMPlaceholder=""
    CultureCurrencySymbolPlaceholder="" CultureDateFormat="" CultureDatePlaceholder=""
    CultureDecimalPlaceholder="" CultureThousandsPlaceholder="" CultureTimePlaceholder=""
    Enabled="True" Mask="99/99/9999" MaskType="Date" TargetControlID="txt_endDate">
</cc1:MaskedEditExtender>
<asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server" ControlToValidate="txt_endDate"
    Display="Dynamic" ErrorMessage="!" ValidationGroup="insertgroup"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValidator2" runat="server" ControlToCompare="txt_startDate"
    ControlToValidate="txt_endDate" Display="Dynamic" ErrorMessage="????? ??????? ??? ?? ???? ??? ?? ????? ???????"
    Operator="GreaterThan" Type="Date" ValidationGroup="insertgroup"></asp:CompareValidator>

编辑: 两个日期的示例会产生问题:

//error message
    start date: 
    28/01/2014

    end date :
    25/07/2014

while

// no error message
    start date: 
    28/01/2014

    end date :
    01/07/2014

i use two CalendarExtender to combine two dates: start date and end date and i use a compare validator to validate that the end date is greater than the start date.

the problem that the validator fires when the day in the end date is smaller than the day in the start date even though the whole end date is greater than the whole start date ..

how to fix this problem?

my aspx:

1-start date:

<asp:TextBox ID="txt_startDate" runat="server" ValidationGroup="insertgroup" MaxLength="10"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txt_startDate"
    ErrorMessage="!" ValidationGroup="insertgroup"></asp:RequiredFieldValidator>
<cc1:MaskedEditExtender ID="txt_startDate_MaskedEditExtender" runat="server" CultureAMPMPlaceholder=""
    CultureCurrencySymbolPlaceholder="" CultureDateFormat="" CultureDatePlaceholder=""
    CultureDecimalPlaceholder="" CultureThousandsPlaceholder="" CultureTimePlaceholder=""
    Enabled="True" Mask="99/99/9999" MaskType="Date" TargetControlID="txt_startDate">
</cc1:MaskedEditExtender>
<cc1:CalendarExtender ID="txt_startDate_CalendarExtender" runat="server" Enabled="True"
    TargetControlID="txt_startDate" Format="dd/MM/yyyy">
</cc1:CalendarExtender>

2-end date:

<asp:TextBox ID="txt_endDate" runat="server" ValidationGroup="insertgroup" MaxLength="10"></asp:TextBox>
<cc1:CalendarExtender ID="txt_endDate_CalendarExtender" runat="server" TargetControlID="txt_endDate"
    Format="dd/MM/yyyy">
</cc1:CalendarExtender>
<cc1:MaskedEditExtender ID="txt_endDate_MaskedEditExtender" runat="server" CultureAMPMPlaceholder=""
    CultureCurrencySymbolPlaceholder="" CultureDateFormat="" CultureDatePlaceholder=""
    CultureDecimalPlaceholder="" CultureThousandsPlaceholder="" CultureTimePlaceholder=""
    Enabled="True" Mask="99/99/9999" MaskType="Date" TargetControlID="txt_endDate">
</cc1:MaskedEditExtender>
<asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server" ControlToValidate="txt_endDate"
    Display="Dynamic" ErrorMessage="!" ValidationGroup="insertgroup"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValidator2" runat="server" ControlToCompare="txt_startDate"
    ControlToValidate="txt_endDate" Display="Dynamic" ErrorMessage="????? ??????? ??? ?? ???? ??? ?? ????? ???????"
    Operator="GreaterThan" Type="Date" ValidationGroup="insertgroup"></asp:CompareValidator>

EDIT:
Example for two dates make problems:

//error message
    start date: 
    28/01/2014

    end date :
    25/07/2014

while

// no error message
    start date: 
    28/01/2014

    end date :
    01/07/2014

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧人九事 2024-11-15 01:00:05

以下是有关为什么您会出现此行为以及为什么 Chad 的代码会对您有所帮助的更多信息。

现在,您的应用程序正在文化设置下运行,该文化设置表示日期应为 mm/dd/yyyy(您的计算机或网络服务器像美国人一样工作)。如果您查看 ASP.NET 为验证器发出的 javascript,您会发现在进行比较时,它首先检查 ControlToValidate 的数据类型是否正确。它通过使用正则表达式解析输入的值来实现此目的,并期望首先是月份值,然后是日期(由于区域性设置)。当它看到您的值为 25 时,它认为这是无效的月份,因此表示您的日期无效。然后它返回 false,甚至不检查第二个字段的日期值或尝试比较它们。

在第二个示例中,验证通过,因为它首先检查结束日期的数据类型,该数据类型可以是有效的 mm/dd/yyyy 日期。然后它检查第二个日期的数据类型,由于 28,导致它认为这不是有效日期。然后验证器返回 true,因为它假定有效日期大于无效日期。

Chad 的代码会将页面线程的区域性设置为使用 dd/MM/yyyy 格式的区域性,因此您的验证器在检查日期是否有效时将使用该格式。

以下是包含详细信息的 MSDN 页面关于文化背景及其所做的事情。

Here's some more information as to why you're getting this behavior, and why Chad's code will help you.

Right now your application is running under a culture setting that says dates should be mm/dd/yyyy (your computer or the webserver is working like an American). If you look at the javascript that ASP.NET emits for the Validators, you'll see that when doing a comparison it first checks whether the datatype of the ControlToValidate is correct. It does this by parsing the entered value with a RegEx, and expects the month value to be first and then the day (because of the culture setting). When it sees your value of 25, it doesn't think this is a valid month, and hence says that your date is not valid. It then returns false, without even checking the second field's date value or trying to compare them.

In your second example, validation passes because first, it checks the datatype of the end date, which CAN be a valid mm/dd/yyyy date. Then it checks the datatype of the second date, which because of the 28, causes it to think it's not a valid date. The validator then returns true, because it assumes that a valid date is greater than a non-valid date.

Chad's code will set the culture of your page's thread to one that uses the dd/MM/yyyy format, and therefore your validators will use that when checking whether your dates are valid.

Here's the MSDN page with more info about the culture settings and what they do.

爱要勇敢去追 2024-11-15 01:00:05

尝试将其放在页面加载的代码后面

System.Globalization.CultureInfo vCulture = (System.Globalization.CultureInfo)System.Globalization.CultureInfo.CreateSpecificCulture("en-GB").Clone(); 
vCulture.DateTimeFormat.ShortDatePattern = "dd/mm/yyyy";    
System.Threading.Thread.CurrentThread.CurrentCulture = vCulture; 

Try putting this in the page load in the code behind

System.Globalization.CultureInfo vCulture = (System.Globalization.CultureInfo)System.Globalization.CultureInfo.CreateSpecificCulture("en-GB").Clone(); 
vCulture.DateTimeFormat.ShortDatePattern = "dd/mm/yyyy";    
System.Threading.Thread.CurrentThread.CurrentCulture = vCulture; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文