Mitesh Vora
Mitesh Vora

Reputation: 458

String was not recognized as a valid DateTime in IIS server 7.5

I have looked at some of questions regarding "String was not recognized as a valid DateTime" but could not get answer. Here is my issue. I have code

DateTime.ParseExact(dr[3].ToString(), "dd-MM-yyyy tt h:m:s", System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat).ToString("MMM. dd yyyy hh:mm tt")

It works fine in localhost but having issues in IIS server. Eg. date in my Sql Server 2008 R2 is "2014-03-05 09:10:17.040" Any help would be appreciated.

Upvotes: 1

Views: 3488

Answers (3)

Soner Gönül
Soner Gönül

Reputation: 98868

Your custom format is wrong. If you use DateTime.ParseExact method, your format and string should match exactly.

Converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly.

Since your string starts with year, you need use yyyy format first. I assume your 03 is month and 05 is day. h format is for 1 to 12. Since your hour is 09, you should use HH format which is for 00 to 23.

m format is ok for this string but is 0 to 59. You might need use mm format which is for 00 to 59. Same situation on your second part.

Also you need fff format for your milliseconds part.

As a result, you should use yyyy-MM-dd HH:mm:ss.fff format instead.

For example;

string s = "2014-03-05 09:10:17.040";
var date = DateTime.ParseExact(s,
                               "yyyy-MM-dd HH:mm:ss.fff",
                               CultureInfo.InvariantCulture);
Console.WriteLine(date);

Output will be;

3/5/2014 9:10:17 AM

Here a demonstration.

After you parse your string, you can format is whatever you want with DateTime.ToString() method like;

date.ToString("MMM. dd yyyy hh:mm tt");

While debugging in asp.net i get format for dr as 17-03-2014 PM 2:53:04

But this doesn't have a same format with example date you write in your question! Anyway, for this case, you can use dd-MM-yyyy tt h:mm:ss format.

For example;

var date = DateTime.ParseExact("17-03-2014 PM 2:53:04",
                               "dd-MM-yyyy tt h:mm:ss",
                               CultureInfo.InvariantCulture);
Console.WriteLine(date);

Unfortunately, looks like your dates don't have a standart date format each others in your database. In this such a case, it is impossible to parsing them all in a one format.

Only you can use DateTime.ParseExact(String, String[], IFormatProvider, DateTimeStyles) overload which you can use more than one format in a string array for your strings.

Upvotes: 1

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26219

Try This:

DateTime.ParseExact(dr[3].ToString(), "yyyy-MM-dd hh:mm:ss.fff", 
  System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat).
  ToString("MMM.dd yyyy hh:mm tt")

Upvotes: 0

Haedrian
Haedrian

Reputation: 4328

You're using ParseExact which will ONLY work if the format of what you're parsing matches the format of the string.

In your code you're expecting "dd-MM-yyyy tt h:m:s"

In the example you gave ("2014-03-05 09:10:17.040"), it's in

yyyy-MM-dd HH:mm:ss

Try changing it to DateTime.Parse() and let it determine the format itself.

Upvotes: 0

Related Questions