Alex
Alex

Reputation: 77349

Force JSON.NET to include milliseconds when serializing DateTime (even if ms component is zero)

I'm using JSON.NET to serialize DateTime values directly from object instances (not using DateTime.ToString() with formatter).

Is there a way to force JSON.NET to include milliseconds in the serialization even if the millisecond component of the DateTime is zero?

Background: I have a very slow web service consumer for this JSON endpoint. Conditional logic is expensive for the consumer, so I'd like to provide the same data formatting every time.

Upvotes: 38

Views: 23849

Answers (3)

Elliveny
Elliveny

Reputation: 2101

I encountered this issue when working with JsonWriter and needed to resolve it because we observed that Javascript date libraries such as Day.js prefer milliseconds to have 3 digits. This question led me to the resolution, so I thought to share it in case others have a use-case like mine:

StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
using (JsonWriter writer = new JsonTextWriter(sw)) {
    writer.Formatting = Newtonsoft.Json.Formatting.None;
    writer.DateFormatString="yyyy'-'MM'-'dd'T'HH':'mm':'ss.fff''";
    writer.WriteStartObject();

    DateTime? date = new DateTime(2021,12,30,23,59,40,250);
    writer.WritePropertyName("Date1");
    writer.WriteValue(date);

    date = new DateTime(2021, 12, 30, 23, 59, 40, 555);
    writer.WritePropertyName("Date2");
    writer.WriteValue(date);

    date = new DateTime(2021, 12, 30, 23, 59, 40, 0);
    writer.WritePropertyName("Date3");
    writer.WriteValue(date);

    date = null;
    writer.WritePropertyName("DateNULL");
    writer.WriteValue(date);
    writer.WriteEndObject();
}
Console.WriteLine(sb.ToString());

This produces:

{
    "Date1": "2021-12-30T23:59:40.250",
    "Date2": "2021-12-30T23:59:40.555",
    "Date3": "2021-12-30T23:59:40.000",
    "DateNULL": null
}

The important line I needed to add was:

writer.DateFormatString="yyyy'-'MM'-'dd'T'HH':'mm':'ss.fff''";

Upvotes: 0

Doug Breaux
Doug Breaux

Reputation: 5115

For any Java people finding this answer and wanting to parse such .NET default-formatted timestamps, I wanted to note a Java 8 (java.time.*) approach for parsing the format:

public static final DateTimeFormatter FORMATTER =
    new DateTimeFormatterBuilder().appendPattern("yyyy-MM-dd'T'HH:mm:ss")
                                  .appendFraction(ChronoField.MILLI_OF_SECOND, 0, 3, true)
                                  .toFormatter();
...
LocalDateTime ldt = LocalDateTime.parse(dateString, FORMATTER);

This says to expect 0-3 digits of fractional milliseconds, with true for an expected decimal point. It correctly interprets, say, ".47" as 470 milliseconds.

Upvotes: 0

Brian Rogers
Brian Rogers

Reputation: 129787

We ran into this same issue on my current project. We are using Web API (and hence JSON.Net) to implement a REST API. We discovered that, when serializing DateTime objects, JSON.Net omits the trailing zeros from the milliseconds, or omits the milliseconds from the date entirely if it is zero. Our clients were expecting a fixed-length date-time string, with exactly 3 digits for the milliseconds. We fixed it by doing the following in Application_Start():

JsonSerializerSettings settings = HttpConfiguration.Formatters.JsonFormatter.SerializerSettings;
IsoDateTimeConverter dateConverter = new IsoDateTimeConverter 
{ 
    DateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.fff'Z'" 
};
settings.Converters.Add(dateConverter);

If you're not using Web API, you can do the same thing by creating a new instance of JsonSerializerSettings, adding the IsoDateTimeConverter to it as shown above, then passing the serializer settings to JsonConvert.SerializeObject().

Note: If you're serializing a DateTimeOffset or a local DateTime and you want to include the timezone offset, replace the quoted 'Z' in the above format with an unquoted K. See Custom Date and Time Format Strings in the documentation for more info.

Upvotes: 63

Related Questions