tags:

views:

84

answers:

3

Hello fellows, I want to convert text to datetime but having error.My text box value is in format dd/MM/yyyy String was not recognized as a valid DateTime.

myFtMaster.GENTRTYDATEFROM =Convert.ToDateTime(txtTreatyPeriodfrom.Text.ToString());

My business object 'gentrtydatefrom' datatype is DateTime. Also what is the best way to avoid these type of errors without using a Try catch block.

+3  A: 

Use something like the following instead:

        DateTime date = DateTime.MinValue;
        DateTime.TryParse(txtTreatyPeriodfrom.Text, out date);

TryParse is similar to DateTime.Parse() but it does not throw an exception if the conversion fails.

If you want to use the en-CA specific culture use the following:

DateTime.TryParse(txtTreatyPeriodfrom.Text, new CultureInfo("en-CA"), 
                DateTimeStyles.AllowWhiteSpaces, out date); 

Then obviously deal with those dates that fail to parse.

Ensuring the user is prompted on the format will obviously reduce mistakes.

Joe R
The `TryParse` methods returns whether the parsing succeded or not. Setting the variable to `DateTime.MinValue` before the call is unneccesary and pointless, as that value is always replaced in the method, regardless if the parsing succeded or not.
Guffa
You are right that date will equal DateTime.MinValue if the parse fails. Setting it is unnecessary but I think it is easier to read for the person maintaining the code. Especially when you add the code that deals with date processing. But anyway, thanks for adding that clarification.
Joe R
+2  A: 

Have a look at

DateTime.TryParse(String value, out DateTime, [culture settings])
ck
+2  A: 

You either specify a culture that uses that specific format:

myFtMaster.GENTRTYDATEFROM = Convert.ToDateTime(
  txtTreatyPeriodfrom.Text, CultureInfo.GetCulture("en-GB")
);

or use the ParseExact method:

myFtMaster.GENTRTYDATEFROM = DateTime.ParseExact(
  txtTreatyPeriodfrom.Text, "dd/MM/yyyy", CultureInfo.Invariant
);

The ParseExact method only accepts that specific format, while the Convert.ToDateTime method still allows some variations on the format, and also accepts some other date formats.

To catch illegal input, you can use the TryParseExact method:

DateTime d;
if (DateTime.TryParseExact(txtTreatyPeriodfrom.Text, "dd/MM/yyyy", CultureInfo.Invariant, DateTimeStyles.None, out d)) {
  myFtMaster.GENTRTYDATEFROM = d;
} else {
  // communcate the failure to the user
}
Guffa
Popo
The conversion returns date in format MM/dd/yyyy although I specify the format like myFtMaster.GENTRTYDATEFROM = DateTime.ParseExact(txtTreatyPeriodfrom.Text.ToString(), "dd/MM/yyyy", new System.Globalization.CultureInfo("en-CA", true));
Popo
ok figured it out, the conversion still depends upon the culture set for the page. Since mine was not set to en-CA so the conversion was perhaps not working correctly.
Popo
@Popo: The conversion returns a `DateTime` value, which doesn't contain any formatting at all, it's just a point in time. When you view the value it will be converted back to a string, and how you view it decides which format is used for that conversion. The conversion from a string to `DateTime` does not depend on the culture set for the page as you specify the culture in the method call, but if you display the `DateTime` value in the page, it will be converted to a string using the culture set for the page.
Guffa
Hi Guffa, this did not worked, it returned the value in format MM/dd/yyyy however in my textbox I had values in dd/MM/yyyy myFtMaster.GENTRTYDATEFROM = DateTime.ParseExact(txtTreatyPeriodfrom.Text.ToString(), "dd/MM/yyyy", new System.Globalization.CultureInfo("en-CA", true));
Popo
@Popo: No, it doesn't return a value in any specific format at all. A `DateTime` value is just a point in time, it's a numeric value describing the number of ticks (1/10000 seconds) since a specific date (epoch).
Guffa
txtTreatyPeriodfrom.Text.ToString() obviously doesn't return the date in the format of "dd/MM/yyyy". Does the textBox also include something like the time? If so, you will get the Format Exception - "String was not recognized as a valid DateTime." Using the code you have it must be exactly "dd/MM/yyyy".
Joe R
Or it could in this format - 2010-1-02. That would also fail.
Joe R