locked
Formatting DateTimes in OData RRS feed

  • Question

  • User1122355199 posted

    Hello everyone and thanks for your help in advance.  I am trying to move into the use of OData and had a couple of questions.  I have previously been using WebApi to send JSON data to various web clients.  The data being returned has several datetime columns as well as columns that need to be formatted into money.  In my current WebApi, I have returned both a datetime value along with a pre-formatted datetime or a pre-formatted money field.  Since I have never used OData or the EntityFramework before, I'm trying to figure out if this can be accomplished or if there is a better way to handle these situations.  Many of the web clients utilize jQuery and don't have really good native support for reformatting datetimes and numbers.  Any insight would be appreciated.

    Thursday, July 28, 2016 10:50 PM

Answers

  • User36583972 posted

    Hi kmcnet,

    You can try to define a MyDateTimeConvertor for Formatting DateTimes in OData. The following code for your reference.

    WebApiConfig:

           JsonSerializerSettings jSettings = new Newtonsoft.Json.JsonSerializerSettings()
                {
                    DateFormatHandling = DateFormatHandling.MicrosoftDateFormat,
                    DateParseHandling = DateParseHandling.DateTime,
                    Formatting = Formatting.Indented,
                    DateTimeZoneHandling = DateTimeZoneHandling.Unspecified
                };
                jSettings.Converters.Add(new MyDateTimeConvertor());
    
                var jsons = new System.Net.Http.Formatting.JsonMediaTypeFormatter();
                jsons.SerializerSettings = jSettings;
                GlobalConfiguration.Configuration.Formatters.Add(jsons);
    
        public class MyDateTimeConvertor : DateTimeConverterBase
        {
            public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
            {
                return DateTime.Parse(reader.Value.ToString());
            }
    
            public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
            {
                writer.WriteValue(((DateTime)value).ToString("yyyy-MM-ddTHH:mm:ss"));
            }
        }

    You could also share us more relevant code to help us reproduce the problem.

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 29, 2016 2:29 AM

All replies

  • User36583972 posted

    Hi kmcnet,

    You can try to define a MyDateTimeConvertor for Formatting DateTimes in OData. The following code for your reference.

    WebApiConfig:

           JsonSerializerSettings jSettings = new Newtonsoft.Json.JsonSerializerSettings()
                {
                    DateFormatHandling = DateFormatHandling.MicrosoftDateFormat,
                    DateParseHandling = DateParseHandling.DateTime,
                    Formatting = Formatting.Indented,
                    DateTimeZoneHandling = DateTimeZoneHandling.Unspecified
                };
                jSettings.Converters.Add(new MyDateTimeConvertor());
    
                var jsons = new System.Net.Http.Formatting.JsonMediaTypeFormatter();
                jsons.SerializerSettings = jSettings;
                GlobalConfiguration.Configuration.Formatters.Add(jsons);
    
        public class MyDateTimeConvertor : DateTimeConverterBase
        {
            public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
            {
                return DateTime.Parse(reader.Value.ToString());
            }
    
            public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
            {
                writer.WriteValue(((DateTime)value).ToString("yyyy-MM-ddTHH:mm:ss"));
            }
        }

    You could also share us more relevant code to help us reproduce the problem.

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 29, 2016 2:29 AM
  • User1122355199 posted

    Thanks for the response.  What I am trying to accomplish is to return both the datetime in native format along with an additional formatted datetime field.  Can that be done with OData and EF?

    Friday, July 29, 2016 1:58 PM
  • User36583972 posted

    Hi  kmcnet,

      Can that be done with OData and EF?

    Yes, you can try it.

    OData is a data access protocol for the web. It provides a uniform way to query and manipulate data sets.

    How to create OData endpoints in Web API.

    http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api

    Best Regards,

    Yohann Lu

    Saturday, August 6, 2016 5:23 AM