Share Blog

Friday, July 06, 2018

Compare date in c#,Asp.net

How to  compare Current system date to Input date?


Function Call

bool resp = CompareDate("06-07-2017");

Function Method

static bool CompareDate(string strDateTime)
{
DateTime dtFinaldate; string sDateTime;
try
{
dtFinaldate = Convert.ToDateTime(strDateTime);
if (dtFinaldate.Year > DateTime.Now.Year)
{
return true;

}
else if (dtFinaldate.Year == DateTime.Now.Year)
{
if (dtFinaldate.Month > DateTime.Now.Month)
{
return true;

}
else if (dtFinaldate.Month == DateTime.Now.Month)
{
if (dtFinaldate.Day >= DateTime.Now.Day)
{
return true;
}
}
}
return false;
}
catch (Exception e)
{
string[] sDate = strDateTime.Split('/');
sDateTime = sDate[1] + '/' + sDate[0] + '/' + sDate[2];
dtFinaldate = Convert.ToDateTime(sDateTime);
if (dtFinaldate.Year > DateTime.Now.Year)
{
return true;

}
else if (dtFinaldate.Year == DateTime.Now.Year)
{
if (dtFinaldate.Month > DateTime.Now.Month)
{
return true;

}
else if (dtFinaldate.Month == DateTime.Now.Month)
{
if (dtFinaldate.Day >= DateTime.Now.Day)
{
return true;
}
}
}
return false;
}
}

No comments:

Post a Comment