TheDizzle
TheDizzle

Reputation: 1574

Cannot apply operator. How would I fix this?

I want to display a message only when the datetime is before or on todays date. Here is what I have:

var todaysdate = DateTime.Today;

if (acct.maturityDate <= todaysdate )           
{
   maturityText.Visible = true;  
}

I get a message saying that (acct.maturityDate <= todaysdate ).

Cannot apply operator '<=' to operands of type 'string' and 'system.datetime', candidates are bool <=(system.datetime,system.datetime) (in struct datetime).

Any help is appreciated.

Upvotes: 0

Views: 1014

Answers (5)

Aaron Palmer
Aaron Palmer

Reputation: 8982

If maturityDate must be left as-is, you can either use the DateTime.Parse, DateTime.TryParse, or DateTime.TryParseExact methods.

Parse will throw an exception if maturity date cannot be parsed.

TryParse and TryParseExact will not throw exceptions, but will allow you to make a decision based on whether the date is able to be parsed.

TryParseExact allows you to parse your date even if it doesn't match a standard DateTime format. You simply specify the format string, as well as culture and style information, in the method parameters.

Parse Example:

var todaysdate = DateTime.Today;

if (DateTime.Parse(acct.maturityDate) <= todaysdate ) {
   maturityText.Visible = true;  
}

TryParse Example:

var todaysdate = DateTime.Today;
DateTime dt;

if (DateTime.TryParse(acct.maturityDate, out dt) 
{
    if (dt <= todaysdate)
    {
       maturityText.Visible = true;  
    }
}

TryParseExact Example:

var todaysdate = DateTime.Today;
DateTime dt;

// use whatever format string matches appropriately
if (DateTime.TryParseExact(acct.maturityDate, "YYYY-MM-dd HH:mm:ss"
                  , CultureInfo.InvariantCulture
                  , DateTimeStyles.None, out dt)
{
    if (dt <= todaysdate)
    {
       maturityText.Visible = true;  
    }
}

Upvotes: 0

Mohamed Farrag
Mohamed Farrag

Reputation: 1692

Firstly you have to change maturityDate to DateTime type, and then you should use DateTime.Compare method you can refer to this link

Upvotes: 0

Ric
Ric

Reputation: 13248

As mentioned in my comment, the type of maturityDate is a string. using DateTime.Parse() would allow you to resolve your issues and so will DateTime.TryParseExact()

Converting to the correct type will allow you to use the correct operators.

Upvotes: 0

Lloyd
Lloyd

Reputation: 29668

As the error says, maturityData is a string and not a DateTime you need to convert it:

var todaysdate = DateTime.Today;

if (DateTime.Parse(acct.maturityDate) <= todaysdate ) {
   maturityText.Visible = true;  
}

I'm making a direct parse there, you might want to consider TryParse or ParseExact depending on your needs.

Upvotes: 7

Abbas
Abbas

Reputation: 14432

This means that the property acct.maturityDate is of the type string and not the expected type System.DateTime. Convert/parse the property to a DateTime and your problem should be solved, or make sure the property is already a DateTime.

Upvotes: 2

Related Questions