Slee
Slee

Reputation: 28248

Cannot convert type 'string' to 'System.DateTime'

Trying to do the following:

order.ExpirationDate =(DateTime) ( ExpMonth + "/" + ExpYear);

ExpMonth, Expyear are both ints.

Upvotes: 1

Views: 6686

Answers (4)

Steve
Steve

Reputation: 8511

Try creating a new DateTime using the constructor which takes month and year as parameters (it also takes a day, but you can default to 1) instead of casting a string, it's much cleaner and easier.

Upvotes: 0

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181270

Try this:

DateTime dt;
if (DateTime.TryParse(ExpMonth + "/" + ExpYear, out dt))
{
   // success
}

Upvotes: 0

Justin Niessner
Justin Niessner

Reputation: 245389

You need to use:

DateTime.Parse(ExpMonth.ToString() + "/" + ExpYear.ToString());

Upvotes: 0

Keltex
Keltex

Reputation: 26426

This is going to be better for you:

order.ExpirationDate = new DateTime(ExpYear, ExpMonth, 1)

Upvotes: 11

Related Questions