如何与"dd/MM/yyyy"格式的日期进行比较 [英] how to compare to dates which are in 'dd/MM/yyyy' format
本文介绍了如何与"dd/MM/yyyy"格式的日期进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我想使用C#
比较asp.net中的日期
我的代码是这样的..
Hi All,
I want to compare the dates in asp.net using C#
my code is like this..
DateTime FromDate = Convert.ToDateTime(txtFromDate.Text);
DateTime ToDate = Convert.ToDateTime(txtToDate.Text);
if (FromDate <= ToDate )
{
------
}
错误:无法将字符串识别为有效的DateTime.
通过使用"MM/dd/yyyy"格式,我们可以获得结果.但是,我的日期格式为"dd/MM/yyyy".
谁能帮我这个忙..
在此先感谢
error: String was not recognized as a valid DateTime.
by using ''MM/dd/yyyy'' format we can get the result. But, my date format is in ''dd/MM/yyyy''.
can any one help me for this..
thanks in advance
推荐答案
使用 DateTime.Compare() [ ^ ]
这是示例代码.
Use DateTime.Compare()[^]
Here is a sample code.
string strFromDate = "26/03/2012";
string strToDate = "26/04/2012";
DateTime FromDate = DateTime.ParseExact(strFromDate, "dd/MM/yyyy",null);
DateTime ToDate = DateTime.ParseExact(strToDate, "dd/MM/yyyy", null);
int result = DateTime.Compare(FromDate, ToDate);
if (result < 0)
{
//from date is less than to date
}
查看此 [ ^ ]链接.
尝试在此处阅读
http://stackoverflow.com/questions/5050102/convert-datetime- to-date-format-dd-mm-yyyy [ ^ ]
Try reading here
http://stackoverflow.com/questions/5050102/convert-datetime-to-date-format-dd-mm-yyyy[^]
这篇关于如何与"dd/MM/yyyy"格式的日期进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文