I have seen this error message problem in stackoverflow, but neither of them was for datetime or date type in order to work with only date type I have made a class of date type and has written some overloads for it in the date class. My date class is
using System;
namespace Common
{
public class Date
{
private DateTime _d1;
public Date(DateTime dateTime)
{
_d1 = dateTime;
}
public static bool operator <(Date date1, Date date2)
{
bool flag = false;
//Now, get the original DateTime Type of C#
DateTime firstDate = Convert.ToDateTime(date1);
DateTime secondDate = Convert.ToDateTime(date2);
//Now compare the two DateTime variables and assign the flag to true
//if the first date is smaller than the second date
int result = DateTime.Compare(firstDate, secondDate);
if (result < 0)
{
flag = true;
}
return flag;
}
public static bool operator >(Date date1, Date date2)
{
bool flag = false;
//Now, get the original DateTime Type of C#
DateTime firstDate = Convert.ToDateTime(date1);
DateTime secondDate = Convert.ToDateTime(date2);
//Now compare the two DateTime variables and assign the flag to true
//if the first date is Greater than the second date
int result = DateTime.Compare(firstDate, secondDate);
if (result > 0)
{
flag = true;
}
return flag;
}
public static bool operator <=(Date date1, Date date2)
{
bool flag = false;
//Now, get the original DateTime Type of C#
DateTime firstDate = Convert.ToDateTime(date1);
DateTime secondDate = Convert.ToDateTime(date2);
//Now compare the two DateTime variables and assign the flag to true
//if the first date is Greater than the second date
int result = DateTime.Compare(firstDate, secondDate);
if (result <= 0)
{
flag = true;
}
return flag;
}
public static bool operator >=(Date date1, Date date2)
{
bool flag = false;
//Now, get the original DateTime Type of C#
DateTime firstDate = Convert.ToDateTime(date1);
DateTime secondDate = Convert.ToDateTime(date2);
//Now compare the two DateTime variables and assign the flag to true
//if the first date is Greater than the second date
int result = DateTime.Compare(firstDate, secondDate);
if (result >= 0)
{
flag = true;
}
return flag;
}
public static bool operator ==(Date date1, Date date2)
{
bool flag = false;
//Now, get the original DateTime Type of C#
DateTime firstDate = Convert.ToDateTime(date1);
DateTime secondDate = Convert.ToDateTime(date2);
//Now compare the two DateTime variables and assign the flag to true
//if the first date is Greater than the second date
int result = DateTime.Compare(firstDate, secondDate);
if (result == 0)
{
flag = true;
}
return flag;
}
public static bool operator !=(Date date1, Date date2)
{
bool flag = false;
//Now, get the original DateTime Type of C#
DateTime firstDate = Convert.ToDateTime(date1);
DateTime secondDate = Convert.ToDateTime(date2);
//Now compare the two DateTime variables and assign the flag to true
//if the first date is Greater than the second date
int result = DateTime.Compare(firstDate, secondDate);
if (result != 0)
{
flag = true;
}
return flag;
}
}//end of class Date
}//End of namespace
but The problem is while I am trying to use in my code behind page its giving me this error — Unable to cast object of type ‘Common.Date’ to type ‘System.IConvertible
the code where I am using it like
Date purchaseDate = new Date(item.PurchaseDate);
Date submissionSate = new Date(item.SubmissionDate);
if (purchaseDate>submissionSate)
{
//to do
}
here in the item object the purchasedate and submision date are datetime properties and the error is in the if line
Can anyone provide me any suggesion? what is the probable solution of this problem?
In your
>operator overload, you haveand there is no overload of
Convert.ToDateTimethat takes yourDateobject, so you are callingConvert.ToDateTime(object), which requiresobjectto implement IConvertible.You can implement
IConvertible, or just compare the_d1values as @ChaosPandion mentions.