Skip to main content

ASP.NET – Custom DateTime Converter for Json.NET to handle non-standard, empty string and null-value date formats

Handling DateTime string-serialized values is often annoying when you’re working on a multi-language project, especially when you’re working with string-based data formats such as XML, CSV, JSON and such. Luckily enough, almost every programming language/framework can provide some native method – or third-party tool – that can solve these issues for you: for .NET and JSON, the most widely used is the exceptional Json.NET by Newtonsoft library, providing unvaluable features such as the SerializeObject and DeserializeObject methods which allows the developer to serialize-deserialize classes of (almost) any kind with ease.

For an extensive guide to Json.NET you can browse the official documentation, full of useful and easy-to-understand samples. In this post, assuming you already know how to effectively use the tool, I’ll talk about a specific DateTime scenario: how to properly de-serialize a JSON input string containing one or more DateTime string representations written in either non-standard format, empty string or null value.

Read More