0

I have string value 7/29/2000. When I convert it to date time it's giving an error. Error Message: Cannot convert string to Datetime

IFormatProvider provider = new System.Globalization.CultureInfo("en-US", true);
string oldValue = decrypt.Decrypt(dtOldI9Value.Rows[0][column.ColumnName].ToString().Trim());
DateTime dtOldValue = DateTime.Parse(oldValue, provider, System.Globalization.DateTimeStyles.NoCurrentDateDefault);

From the 3 rd line i am getting the value as "7/29/2000". Please help me.

6
  • 1
    what does decrypt do ? Have you stepped into line 3 to see what's actually inside oldValue ? Commented Jul 3, 2012 at 12:07
  • Put a break point on third line and see what is inside oldValue Commented Jul 3, 2012 at 12:07
  • 3rd line oldValue="07/29/2000" Commented Jul 3, 2012 at 12:08
  • Try to log dtOldI9Value.Rows[0][column.ColumnName].ToString().Trim() to see if the date is well formatted Commented Jul 3, 2012 at 12:12
  • Sorry for the wrong value.Actual value is "7/29/2000" Commented Jul 3, 2012 at 12:15

1 Answer 1

3

This works:

IFormatProvider provider = new System.Globalization.CultureInfo("en-US", true);
DateTime dtOldValue = DateTime.Parse("07/29/2000", provider, System.Globalization.DateTimeStyles.NoCurrentDateDefault);
Console.WriteLine(dtOldValue);

So there must be something with your decrypt.Decrypt(). Are you sure it doesn't adds characters to oldValue? Check oldValue.Length, it should be 10.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 for the lenght check. With a method called decrypt, is there a possibility for none-displayable characters in the string?
Actual value is "7/29/2000" with 9 length

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.