Parsing a UTC string from a weblog into a DateTime

S

Sterling Ledet

I am trying to create a web service that takes a string from my web server
in the following format:

Mon, 6 Oct 2003 18:39:47 UTC

and put's in a datetime so it can then be reformatted in C# as necessary
for variuos purposes.

There is something wrong with the following code. I get the following error
when I enter in the above date string:

System.FormatException: String was not recognized as a valid DateTime.
at System.DateTimeParse.ParseFormatError(Boolean isThrowException, String
resourceID)
at System.DateTimeParse.DoStrictParse(String s, String formatParam,
DateTimeStyles styles, DateTimeFormatInfo dtfi, Boolean isThrowExp,
DateTime& returnValue)
at System.DateTimeParse.ParseExact(String s, String format,
DateTimeFormatInfo dtfi, DateTimeStyles style)
at System.DateTime.ParseExact(String s, String format, IFormatProvider
provider)
at DateTest.Service1.TestDate(String inputDate) in
\\commerce.ledet.com\wwwroot$\datetest\service1.asmx.cs:line 69
Can anyone tell me what's wrong with the code below??

[WebMethod]

public string TestDate(string inputDate)

{

// Define a custom string format to display the DateTime value.

// zzzz specifies the full time zone offset.

String expectedFormat = "ddd, d MMM yyyy hh:mm:ss";

System.DateTime ouputDateTime =

System.DateTime.ParseExact(inputDate, expectedFormat, null);

return ouputDateTime.ToString();

}
 
B

Brian W

There is no direct way to do the conversion.

It's the "UTC" in the string causing the problem.

Will this work for you?

public string TestDate(string inputDate)
{
// Define a custom string format to display the DateTime value.
// zzzz specifies the full time zone offset.

string myDate = inputDate.Replace("UTC", string.Empty);
System.DateTime ouputDateTime
= System.DateTime.Parse(myDate);

return ouputDateTime.ToString();
}

Hope this helps.
Brian W
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top