kumar
kumar

Reputation: 2944

Converting the string to datetime using asp.net

string Edate = collection["effectiveDatePicker"].ToString();
string Cdate = collection["cancelDatePicker"].ToString();

The Date I am getting is 20101112 or 20101116

Then I am doign soemthign like this to assing my Datetime variable

h.Edate= DateTime.ParseExact(collection["effectiveDatePicker"].ToString(), "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture, DateTimeStyles.NoCurrentDateDefault);
                        h.CDate= DateTime.ParseExact(collection["cancelDatePicker"].ToString(), "yyyyMMdd", System.Globalization.CultureInfo.CurrentCult

After saving to the data base.. I am seeing the EDate and CDate fileds somethign like this

11/10/2010
11/15/2010

with the same dates when I Submit again I am getting Error in the ParseExact bec the string I am getting is 11/10/2010 but its expecting 20101010

Can any body help me out?

Upvotes: 0

Views: 418

Answers (3)

GendoIkari
GendoIkari

Reputation: 11914

You can specify the format that your dates are coming in as like this:

string Edate = collection["effectiveDatePicker"].ToString("yyyyMMdd");
string Cdate = collection["cancelDatePicker"].ToString("yyyyMMdd");

This should ensure that you are working with strings that look like what you want.

Upvotes: 2

Joshua Smith
Joshua Smith

Reputation: 1004

I'm not sure what your specific question is. Because you're using DateTime.ParseExact() and are specifying a format of 'yyyyMMdd', passing in a string such as '11/04/2010' will fail.

Upvotes: 0

usr-local-ΕΨΗΕΛΩΝ
usr-local-ΕΨΗΕΛΩΝ

Reputation: 26874

I can tell you for sure that there is a reformatting problem. I don't fully understand your code and the steps it takes (I guess you are using data binding or something similar).

The point seems that dates are initially set as yyyyMMdd, but when you postback the ToString() operator is applied to these dates, converting them to your OS's native format MM/dd/yyyy.

You must force them to be converted into yyyyMMdd again, because by default ToString will use CurrentUICulture which is not good for you.

You should show us the update code

Upvotes: 1

Related Questions