The dateTimeInvalidLocalFormat MDA is activated when a DateTime instance that is stored as a Universal Coordinated Time (UTC) is formatted using a format that is intended to be used only for local DateTime instances. This MDA is not activated for unspecified or default DateTime instances.
An application is manually serializing a UTC DateTime instance using a local format:
DateTime myDateTime = DateTime.UtcNow; Serialize(myDateTime.ToString("yyyy-MM-dd'T'HH:mm:ss.fffffffzzz"));
The 'z' format for the DateTime.ToString method includes the local time zone offset, for example, "+10:00" for Sydney time. As such, it will only produce a meaningful result if the value of the DateTime is local. If the value is UTC time, DateTime.ToString includes the local time zone offset, but it does not display or adjust the time zone specifier.
UTC DateTime instances should be formatted in a way that indicates that they are UTC. The recommended format for UTC times to use a 'Z' to denote UTC time:
DateTime myDateTime = DateTime.UtcNow; Serialize(myDateTime.ToString("yyyy-MM-dd'T'HH:mm:ss.fffffffZ"));
DateTime myDateTime = DateTime.UtcNow; Serialize(myDateTime.ToString("o"));
There is no special output as a result of this MDA activating., However, the call stack can be used to determine the location of the ToString call that activated the MDA.
DateTime myDateTime = DateTime.UtcNow; String serialized = XMLConvert.ToString(myDateTime);
For this specific example, pass in XmlDateTimeSerializationMode.RoundtripKind to the ToString call on XmlConvert. This serializes the data as a UTC time.
DateTime myDateTime = DateTime.UtcNow; String serialized = XmlConvert.ToString(myDateTime, XmlDateTimeSerializationMode.RoundtripKind);