aas
aas

Reputation: 197

Datetime format is miss matching in c# when hosting

I had a situation that locally working but not in hosting (godaddy.com)

DateTime date = DateTime.Parse(TextBoxSelectedDate.Text.Trim());

string d = date.ToString("yyyy-MM-dd");//localhost out put 2013-10-13

 DateTime.Parse(d);

but when I host is giving this exception

System.FormatException: String was not recognized as a valid DateTime.

Why is this

Thanks

Upvotes: 0

Views: 573

Answers (2)

Jeppe Stig Nielsen
Jeppe Stig Nielsen

Reputation: 61952

You use a format string when you go from DateTime to string. Use the same format string when you go in the opposite direction:

DateTime.ParseExact(d, "yyyy-MM-dd", null)

If you want to be absolutely sure the current culture of your thread doesn't mess things up, you can use the invariant culture:

DateTime.ParseExact(d, "yyyy-MM-dd", CultureInfo.InvariantCulture)

Upvotes: 1

aL3891
aL3891

Reputation: 6275

Your hoster might be using a different language / region setting than you. Try calling Parse with a specific culture object:

DateTime.Parse("2013-10-13", CultureInfo.GetCultureInfo("the name of your culture, en-US for example"))

You can find a list of culture names here: http://msdn.microsoft.com/en-us/goglobal/bb896001.aspx

You can also use CultureInfo.InvariantCulture

Upvotes: 1

Related Questions