By default, JSON.NET will omit the milliseconds component of a DateTime
value if it is zero. If you want to include the milliseconds component even if it is zero, you can use a custom JsonConverter
to override the serialization behavior.
Here's an example of a custom JsonConverter
that includes the milliseconds component of a DateTime
value:
using System; using Newtonsoft.Json; using Newtonsoft.Json.Converters; public class DateTimeWithMillisecondsConverter : IsoDateTimeConverter { public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { DateTime dateTime = (DateTime)value; string formattedDateTime = dateTime.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffzzz"); writer.WriteValue(formattedDateTime); } }
In this example, a custom DateTimeWithMillisecondsConverter
class is defined that inherits from IsoDateTimeConverter
. The WriteJson
method is overridden to format the DateTime
value as a string that includes the milliseconds component, even if it is zero.
To use the custom converter, you can add it to the Converters
collection of your JsonSerializerSettings
object:
using System; using Newtonsoft.Json; using Newtonsoft.Json.Converters; // ... DateTime dateTime = new DateTime(2023, 4, 20, 15, 30, 0); var serializerSettings = new JsonSerializerSettings { Converters = new[] { new DateTimeWithMillisecondsConverter() } }; string json = JsonConvert.SerializeObject(dateTime, serializerSettings);
In this example, a DateTime
value is created and a new JsonSerializerSettings
object is created with the DateTimeWithMillisecondsConverter
added to its Converters
collection. The JsonConvert.SerializeObject
method is then called with the DateTime
value and the custom JsonSerializerSettings
object to serialize the DateTime
as a JSON string that includes the milliseconds component.
By using a custom JsonConverter
like this, you can force JSON.NET to include the milliseconds component of a DateTime
value even if it is zero.
"JSON.NET serialize DateTime with milliseconds zero"
JsonConvert.SerializeObject(dateTime, new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat, Formatting = Formatting.None });
JsonSerializerSettings
to include milliseconds in the serialized DateTime, even if the millisecond component is zero."C# JSON.NET serialize DateTime milliseconds format"
JsonConvert.SerializeObject(dateTime, new JsonSerializerSettings { DateFormatString = "yyyy-MM-ddTHH:mm:ss.fffZ", Formatting = Formatting.None });
"JSON.NET serialize DateTime milliseconds always"
JsonConvert.SerializeObject(dateTime, new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat, Formatting = Formatting.None, DateTimeZoneHandling = DateTimeZoneHandling.Utc });
DateFormatHandling
to MicrosoftDateFormat
and using UTC."JSON.NET serialize DateTime milliseconds zero"
JsonConvert.SerializeObject(dateTime, new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.IsoDateFormat, Formatting = Formatting.None, DateTimeZoneHandling = DateTimeZoneHandling.Utc });
"C# JSON.NET serialize DateTime milliseconds format zero"
JsonConvert.SerializeObject(dateTime, new JsonSerializerSettings { DateFormatString = "yyyy-MM-ddTHH:mm:ss.fffZ", Formatting = Formatting.None });
"C# JSON.NET serialize DateTime milliseconds include zero"
JsonConvert.SerializeObject(dateTime, new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.IsoDateFormat, Formatting = Formatting.None });
"C# JSON.NET serialize DateTime milliseconds zero always"
JsonConvert.SerializeObject(dateTime, new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.IsoDateFormat, Formatting = Formatting.None, DateTimeZoneHandling = DateTimeZoneHandling.Utc });
"C# JSON.NET serialize DateTime milliseconds zero format"
JsonConvert.SerializeObject(dateTime, new JsonSerializerSettings { DateFormatString = "yyyy-MM-ddTHH:mm:ss.fffZ", Formatting = Formatting.None, DateTimeZoneHandling = DateTimeZoneHandling.Utc });
"JSON.NET serialize DateTime milliseconds format zero always"
JsonConvert.SerializeObject(dateTime, new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.IsoDateFormat, Formatting = Formatting.None, DateTimeZoneHandling = DateTimeZoneHandling.Utc });
"C# JSON.NET serialize DateTime milliseconds zero ISO format"
JsonConvert.SerializeObject(dateTime, new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.IsoDateFormat, Formatting = Formatting.None });
ipad dom-events subplot dynamic-memory-allocation firefox mongoid collocation websocket resnet sha256