469,133 Members | 1,032 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,133 developers. It's quick & easy.

Parsing a UTC string from a weblog into a DateTime

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.as mx.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();

}
Nov 15 '05 #1
2 12489
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
"Sterling Ledet" <sj*****@ledet.com> wrote in message
news:e$**************@TK2MSFTNGP12.phx.gbl...
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.as mx.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();

}

Nov 15 '05 #2
Add UTC on the format specs:
"ddd, d MMM yyyy hh:mm:ss UTC"

Cheers!
--
/jop
"Sterling Ledet" <sj*****@ledet.com> wrote in message
news:e$**************@TK2MSFTNGP12.phx.gbl...
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.as mx.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();

}

Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Gerrit Holl | last post: by
9 posts views Thread by Thomas W | last post: by
2 posts views Thread by Simon | last post: by
50 posts views Thread by z. f. | last post: by
reply views Thread by Uncle Leo | last post: by
8 posts views Thread by thunderbolt | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.