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



Question:

Trying to do the following:

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

ExpMonth, Expyear are both ints.


Solution:1

This is going to be better for you:

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


Solution:2

You need to use:

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


Solution:3

Try this:

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


Solution:4

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »