1

I seem to be missing something somewhere. Hopefully someone can spot it and I can get on with my life safe in the knowledge that I need to look harder in future:

I have been trying to configure a WebApi 2 project, which uses JSON.net serialiser.

EDIT: my endpoints are odata 2 - I am pursuing if this makes a difference.

I want the DateTime format to universally be something other than ISO. I understand that the ISO format is the ideal one for the purpose, however at the moment my client library is not handling this well, and for a quick-and-dirty I want to exchange Dates and DateTimes using the same format as required for display/edit. This will buy me the time to write the customised modifications of the client library's Date/DateTime controls. My scenario usefully does not call for actual localisation so in the short term I am safe to use this approach.

So, having followed various threads and guides including: http://james.newtonking.com/archive/2009/02/20/good-date-times-with-json-net

Custom DateTime serialization with Json.Net

Set default global json serializer settings

I still do not seem to be able to get anything back other than the ISO date format. I have toggled the 'Formatting Indented' back and forth and I can see that is affecting my result fine.

Here is the config I am currently applying in my WebApiConfig.cs Register method. Can anyone help me with making the right configuration so that ALL Dates and Times are returned in either Javascript format, or using a custom JSONConverter for UK date format (dd/mm/yyyy HH:mi:ss):

    JsonMediaTypeFormatter jsonFormatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
    JsonSerializerSettings jSettings = new Newtonsoft.Json.JsonSerializerSettings()
    {
        DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat,
        DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc,
        NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore,
        Formatting = Newtonsoft.Json.Formatting.Indented, // For dev only set json to prettyprint
        Culture = new CultureInfo("en-GB"),
    };
    //jSettings.Converters.Add(new UkDateTimeConvertor());
    //jSettings.DateFormatString = "dd'-'MM'-'yyyy' 'HH':'mm':'ss";
    jSettings.Converters.Add(new JavaScriptDateTimeConverter());
    jsonFormatter.SerializerSettings = jSettings;
1
  • I long since answered my own question on this: odata endpoints use a different serialiser, so setting Newtonsoft.Json settings doesn't make any difference. Commented Jul 5, 2019 at 12:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.