I have recently been working on a project where I have the following scenario:
- Receive a flat file from system X.
- Map the flat file to an internal message schema.
- Send the mapped message to a web service in system Y.
Three very basic steps, I am sure you will agree. I did however run in to a problem with date formatting. I had agreed with the developers of
system Y, that thye shold be using
System.DateTime for their dates and not worrying about the format required by
system X. When serializing, this was fine and we got the dates that BizTalk was happy with.
I.e. 2007-10-26T17:54:57.437
Great, I could convert this to the formats i needed for
system X in the maps using some custom functiods. My problem was that in the other direction, it was quite difficult to create a date format as above. I tried all of the string methods on the System.DateTime class and didn't really get anywhere. I would get the error "
The string '26/10/2007 17:54:57' is not a valid AllXsd value". Luckily, I happened across
this post on
The Scripts.
Essentially, what is is saying is that if you use the
.ToString() method witha parameter of
"s", then this will format the string according to
ISO-8601, which is what the
XML Schema Date is based on, and what .Net uses for serialization.
So, using
DateTime.Now.ToString("s");
will result in
2007-11-28T12:22:15.041
Hurrah!