Skip to content

How do I convert an XML DateTime in a web method

Terry Showers

Asked Aug 26, 2010, 10:18 AM EDT2 answers
Share
LinkedInX
I am passing a string into a webservice that contains a date formatted as "2010-08-26T00:00:00".  this format is required by the infoPath form that passes this through XML.

When I try to convert the date into a usable date in the web method I get an error "System.FormatException: String was not recognized as a valid DateTime" when I run the application (InfoPath form).

The partial code for the web method looks like:

public string CheckDuplicates(string StartDate)
   {
     DateTime datStartDate = DateTime.Parse(StartDate);


(the error occurs on the first line of the method doing the parse)

The code works correctly if I hard code the line like:

DateTime datStartDate = DateTime.Parse("2010-08-26T00:00:00");

The application is an InfoPath form that calls this web service and passes the string, I can view the string and verify that it matches the format I indicated previously but the web method gets confused.

Is there something wrong in my code or the way I am using Parse?

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Suthish Nair
    0
    They are called SortableDateTime.
    Long time back i faced same issue, but sorted out below way.
    Not found any other conversion code. or wait for more replys..

    Convert.ToDateTime(("2010-08-26T00:00:00").Replace("T", " ")).ToString("dd-MMM-yyyy hh:mm:ss tt")

    for more conversions: http://www.csharp-examples.net/string-format-datetime/
    0
  • 0
    It looked like your solution might work, but when I tried it I got the same error.
    What I have learned in the process of trying to figure this out is I can apparently pass the date (as a string) to the web service whitout any problem.  I can also return a value so I can see the results of whatever operation I want to perform, so if I return the same string I just passed in ("2010-08-26T00:00:00") I can see the string is correct.  I can also return the length of the string and that is correct (19), but if I do any operation that tries to manipulate the string like a convert or extracting a substring I get an error.
    For example if I do
    string NewString = StartDate.Substring(1,4);
    I see the error "System.ArgumentOutOfRangeException: startIndex cannot be larger than length of string." when I have just seen that the length of the string is 19.
    It seems like I have a string that is not a string.  I am very confused!
    I need to reiterate what I refered to before, If I invoke the WSDL I don't see the error, but if I run the InfoPath form that passes the same string, that is where the issue occurs.  Is the string coming through XML different somehow?
    0