如何进行日期比较 [英] How to do the date comparison

查看:96
本文介绍了如何进行日期比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想检查大于当前日期的日期的有效性.我正在使用包含日期值的文本框.当我点击提交按钮时,它应该检查输入的日期是否大于当前日期,这是代码:

Hi,

I want to check the validation for the date that is greater than the current date. I am using the text box that contains the date values. When I click the submit button it should check the validation whether the entered date is greater than the current date or not.Here is the code:

DateTime EnquiryDate= UserUtil.ConvertDate(txtEnquiryDate.Text);//Here I am getting the error string cannot be converted to dateTime.
if(EnquiryDateEnquiryDate>=DateTime.Now)



txtEnquirydate包含:2011年4月8日.
我正在使用转换日期功能,并将该日期转换为MM/DD/YYYY格式.

在此先感谢



Here the txtEnquirydate contains:04/08/2011.
I am using the convert date function and converting that date to MM/DD/YYYY format.

Thanks in advance

推荐答案

我不知道您的UserUtil.ConvertDate是如何实现的,但是问题就在那里. 您需要做的不是转换".这是解析为System.DataTime的文本.

只需使用方法System.DateTime.ParseSystem.DateTime.ParseExact之一.您可以指定文本或区域性的预期格式(因为CultureInfo在某些方法中将IFormatProvider实现为参数).

请参阅 http://msdn.microsoft.com/en-us/library/system.datetime.aspx [^ ] .

您将在这里找到代码示例:
http://msdn.microsoft.com/en-us/library/kc8s65zs.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/ey1cdcx8.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/ms131038.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/332de853.aspx [ ^ ].

您的比较是正确的;始终使用比较运算符.您还可以使用减法运算符(-")在时间点之间获取System.TimeSpan.

—SA
I have not idea how your UserUtil.ConvertDate is implemented, but the problem is there.
What you need to do is not "convert"; this is parsing some text into System.DataTime.

Just use one of the methods System.DateTime.Parse or System.DateTime.ParseExact. You can specify expected format of the text or the culture (as CultureInfo implements IFormatProvider expected as a parameter in some of those methods).

See http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^].

You will find code samples here:
http://msdn.microsoft.com/en-us/library/kc8s65zs.aspx[^],
http://msdn.microsoft.com/en-us/library/ey1cdcx8.aspx[^],
http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx[^],
http://msdn.microsoft.com/en-us/library/ms131038.aspx[^],
http://msdn.microsoft.com/en-us/library/332de853.aspx[^].

Your comparison is correct; always use comparison operators. You can also use subtraction operator ("-") to get System.TimeSpan between to points in time.

—SA


如何使用ASP.NET AJAX Calendar Extender
http://www.dotnetcurry.com/ShowArticle.aspx?ID=149 [ ^ ]
http://stackoverflow.com/Questions/5608062/how-to-dis-disable-pre-date-in-calendarextender-control-through-its-render-even [
How about using ASP.NET AJAX Calendar Extender
http://www.dotnetcurry.com/ShowArticle.aspx?ID=149[^]
http://stackoverflow.com/questions/5608062/how-to-disable-previous-dates-in-calendarextender-control-through-its-render-even[^]

may help you


这篇关于如何进行日期比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆