Conversion from string to date type is not valid.

I have the following date as string "091020". I am using SubSonic as my DAL. When i do

myObject.DateColumn = "091020"

      

I am getting the error "Conversion from String to Type Date is invalid"

I tried playing with IFormatProvider and CultureInfo but couldn't seem to hit the error.

What am I missing?

+2


source to share


2 answers


Try

myObject.DateColumn = DateTime.ParseExact("091020", "yyMMdd", null)

      



DateTime.ParseExact lets you specify exactly how to parse a date when it isn't entirely obvious.

+5


source


.Net cannot make out this as of a valid date. Throw this into a new project and make sure:

string sDate = "091120";
DateTime dt = DateTime.MinValue;

if (DateTime.TryParse(sDate, out dt))
    MessageBox.Show(dt.ToShortDateString());
else
    MessageBox.Show("Nope");

      

So, if you KNOW that all your dates are yyMMdd, then use this:



DateTime.ParseExact("091120", "yyMMdd", null)

      

How do you get these dates? Are they guaranteed to be 6 digits in yyMMdd format?

+2


source







All Articles