Problem
I was wanting to take a string and map it to a date, if the string was not a valid date then i wanted to return the min date value. i had this seemingly working okay but the problem was i was getting an error that the date was not a compatible xsd date.
Solution
Im sure there will be other ways of doing this, but i found a handle little class in the System.Runtime.Remoting.Metadata.W3cXsd2001 namespace called SoapDateTime. Basically it has a handly static method of ToString which will take a .net DateTime and return an xsd compatible date time string. There is a code snippet below.
public string ConvertWithDefaultMin(string inputDate)
{
DateTime requiredDate;
bool parsed = DateTime.TryParse(inputDate, out requiredDate);
if (!parsed)
{
string message = string.Format(CultureInfo.InvariantCulture, "The input date string {0} could not be parsed", inputDate);
InstrumentationHelper.LogWarning(EventNumbers.DateTimeFormatterCanNotParseDate, Constants.ApplicationName, message);
}
return SoapDateTime.ToString(requiredDate);
}