4

I get the following error message in my C# program in the statement:

dr["StartDate"] = Convert.ToDateTime(dr["business_dt"]).ToString("MM/dd/yyyy");

I do not get this error on my US machine.But it throws an error on the user's machine located outside the US. The dateformat being returned from datareader is: 08/31/2010 12:00:00 AM

System.FormatException: String was not recognized as a valid DateTime. at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles) at System.Convert.ToDateTime(String value, IFormatProvider provider) at System.String.System.IConvertible.ToDateTime(IFormatProvider provider) at System.Convert.ToDateTime(Object value)

Please advise.

Thanks.

1
  • 2
    Why does your DataRow contain strings anyway, instead of DateTime values? Commented Oct 14, 2011 at 10:24

4 Answers 4

4

Edit:

use DateTime.ParseExact method:

var dateString = dr["business_dt"].ToString();
var format = "MM/dd/yyyy hh:mm:ss tt";
var dateTime = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture);
dr["StartDate"] = dateTime;
Sign up to request clarification or add additional context in comments.

7 Comments

I tried it and it doesn't work: IMHO you should use var format = "MM/dd/yyyy hh:mm:ss tt"
@Davide,@Marco,This did not work for me.Still get the error: "input string was not in correct format".I tried this one too and it gives the same error: DateTime.ParseExact(DateTime.Now.ToString(), "MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);Thanks.
yeah, use the example in my answer and not this your code from the comment above, it's different...
No Davide, you edited the wrong part I think: you should do var dateString = dr["business_dt"].ToString(); var format = "MM/dd/yyyy hh:mm:ss tt"; and then the remaining part of the code!!
You assume that dr["business_dt"] is of type DateTime. I think it is of type string.
|
3

The problem is that in several countries the month and the day part of the date are turned. In US it is "MM/dd/yyyy", in e.g. germany it is "dd/mm/yyyy".

So you have to specify what format your string is in (in your case it looks like the US format so I choosed this culture):

DateTime date = Convert.ToDateTime(dr["business_dt"], new CultureInfo("en-US"));
dr["StartDate"] = date.ToString("MM/dd/yyyy");

1 Comment

Please post a comment when downvoting.
0

This is happening because the default culture of the machine says that the date format is "dd/MM/yyyy" and in your case, the month is going in as "31" which is incorrect that is why it throws error.

Please set the culture prior to parsing the datetime.

use this code before your code:

CultureInfo CultureInfo1 = (CultureInfo)CultureInfo.CurrentCulture.Clone();
CultureInfo1.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
Thread.CurrentThread.CurrentCulture = CultureInfo1;

let me know if this helps.

Comments

0

I've just had a related problem in ASP.NET. Issue was occurring on ONE web site on a web server running TWO (almost identical) copies of the code. Had me going for a while, as no logic seemed to fit the symptoms. It turned out to be an issue with the Web.Config. The failing site was missing:

<globalization uiCulture="en" culture="en-AU" /> from the <system.web> section.

Thought I'd just mention this in case it helps anyone.

Comments

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.