How do I solve the DateTimeInvalidLocalFormat error: "UTC DateTime is converted to text in a format that is only suitable for local times."?

I am getting this error in Debug while ToString () is being executed:

A UTC DateTime is converted to text only in a format that is correct for local time. This can happen when calling DateTime.ToString using the "z" format specifier, which will include the local timezone offset in the output. In this case, use either "Z", a format specifier that denotes UTC time, or uses the format "o" string, which is the recommended way to store DateTimes in text. This can also happen when passing a DateTime to serialize an XmlConvert or DataSet. If you are using XmlConvert.ToString then go to XmlDateTimeSerializationMode.RoundtripKind for proper serialization. If using a DataSet, set the DateTimeMode in the DataColumn to DataSetDateTime.Utc.

public static string ToInterfaceString(this DateTime value)
{
    return value != DateTime.MinValue ? value.ToString("yyyy-MM-ddTHH:mm:sszzz") : string.Empty;
}

      

The application I'm just starting out uses this format in many places. So what should I really do? Replace zzz with Z?

Update 1: The DateTime that is passed to my extension is triggered:

DateTimeCreated = DateTime.UtcNow;

      

The weird thing is that if I go to this extension, some other DateTime objects, I don't get any errors / warnings.

+3
c # datetime


source to share


1 answer


This is only a green warning.



So, as you seem to know what you are doing, you can simply understand the message (that's correct) and check the box to ignore this warning in the future.

0


source to share







All Articles