James Hughes James Hughes - 1 year ago 87
ASP.NET (C#) Question

DateTime.Parse works in c# console app but not asp.net

I have written a C# console application that after all of it's processing outputs a

to disk. It does this like so:


This same console appication has no problems with using the following to read this


However, upon attempting to use the following code in my separate
program I recieve an error saying that my
is not a valid
which is odd to say the least.

StreamReader reader = new StreamReader(@"D:\InformerReports\Archive\ReliabilityData\StartTime.hist");
string dateString = reader.ReadLine();
return DateTime.Parse(dateString);

I have checked and the string it is reading in is
10/25/2016 12:00:00 AM
I have also attempted to use
return DateTime.ParseExact(dateString, "dd/MM/yyyy hh:mm:ss tt",null)
but this returns the same error.

I can't seem to fathom why identical code performed on the same file works in one case and not the other. I'd appreciate some help.

Answer Source

I guess the culture of the server is different from the machine you are testing from.

The correct format you have to use seems to be:

return DateTime.ParseExact(dateString, "MM/dd/yyyy hh:mm:ss tt", null)
//                                      10/25/2016 12:00:00 AM
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download