James Hughes James Hughes - 1 month ago 15
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

DateTime
to disk. It does this like so:

writer.WriteLine(myDateTime)
.

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

DateTime.Parse(reader.ReadLine())


However, upon attempting to use the following code in my separate
Asp.Net
program I recieve an error saying that my
string
is not a valid
DateTime
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

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