Friday, February 8, 2008

Converting DateTime Object from .NET to XSD

if you are trying to consume a Java Web Service in .NET simply follow the instruction in Consuming a Web Service in .NET, you might run into some issues when dealing with DateTime Object. You want to make sure that the format of DateTime being sent to the Web Service(WS) is the one it expects. The WS i was working with required the DateTime in XSD format.

You can use XmlConvert or SoapDateTime class to convert the date to a string, but the problem is the the format they return is "2007-12-14T10:37:00.0000000-05:00" and the actual XSD format is "2007-12-14T10:37:00.000-05:00". There are 4 extra zeros. So i wrote a simple method to convert .NET DateTime Object to XSD format.

public String toXSDDateFormat(DateTime d)
{
String strD = XmlConvert.ToString(d);
String d1 = strD.Substring(0,strD.LastIndexOf(".")+4);
String d2 = strD.Substring(strD.LastIndexOf("-"),strD.Length -
strD.LastIndexOf("-"));

return d1+d2;
}

Hope this helps !!

No comments: