Author Topic: DateTime problem  (Read 5724 times)

daan

  • Newbie
  • *
  • Posts: 6
    • View Profile
DateTime problem
« on: September 29, 2014, 12:43:36 pm »
Found another problem that I haven't been able to solve yet.
I have a list of objects that contains

Code: [Select]
public DateTime StartDate { get; set; }
Then I used your example code to the list back from MVC

Code: [Select]
StringBuilder sb = new StringBuilder(@"{""totalRecords"":" + total_Records + @",""curPage"":" + pq_curPage + @",""data"":");

JavaScriptSerializer js = new JavaScriptSerializer();

String json = js.Serialize(orders);
sb.Append(json);
sb.Append("}");

return this.Content(sb.ToString(), "text/text");

In the grid I added a column with these settings

Code: [Select]
title: "Start Date", minWidth: "190", dataIndx: "StartDate", dataType: "date"
When I view the page, instead of getting the date I get the serialized version

Code: [Select]
/Date(1288566000000)/
Any idea why? How can get the deserialized date?

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6210
    • View Profile
Re: DateTime problem
« Reply #1 on: September 29, 2014, 03:18:53 pm »
you would need to implement custom json converter.

    public class invoiceConvertor : JavaScriptConverter
    {
        //private const string _dateFormat = "MM/dd/yyyy";
        private const string _dateFormat = "yyyy-MM-dd";

        public override IEnumerable<Type> SupportedTypes
        {
            get
            {
                return new[] { typeof(Invoice) };
            }
        }

        public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
        {
            //we are not using Deserialize for this example.
            return new Object();
        }

        public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
        {
            IDictionary<string, object> serialized = new Dictionary<string, object>();

            var order = (Invoice)obj;
            foreach (var prop in order.GetType().GetProperties())
            {
                if (prop.PropertyType == typeof(DateTime?))
                {
                    DateTime? val = (DateTime?)prop.GetValue(order, null);
                    serialized[prop.Name] = (val == null) ? "" : ((DateTime)val).ToString(_dateFormat);                   
                }
                else
                {
                    serialized[prop.Name] = prop.GetValue(order, null);
                }
            }           

            return serialized;
        }
    }


       JavaScriptSerializer js = new JavaScriptSerializer();
       js.RegisterConverters(new[] { new invoiceConvertor() });

daan

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: DateTime problem
« Reply #2 on: September 29, 2014, 07:57:58 pm »
That worked! Thanks!

Is this the only way to do it? Lets say if we have a huge system with hundreds of grids connected to hundreds of classes, we would have to do a custom converter for every class? I've used other grids in the past, but never encountered this behavior before. Still, I like this grid and it's not a breaking point, just would be great to have something like this built into the grid :D

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6210
    • View Profile
Re: DateTime problem
« Reply #3 on: September 30, 2014, 12:52:12 pm »
This is not the only way.

You can also do it client side in dataModel.getData callback, iterate over the data and convert all data where column.dataType=="date"

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6210
    • View Profile
Re: DateTime problem
« Reply #4 on: October 20, 2014, 05:02:01 pm »
yet another way to do it on the server side is by use of JSON.NET serializer.

http://james.newtonking.com/archive/2009/02/20/good-date-times-with-json-net