2

I have problems transmitting a Date (in a String Format) into our object, since the serializer seems to convert it wrong all the time.

The format is actually dd.MM.yyyy (Swiss time format) and the serializer tries to convert it to MM-dd-YYYY (where the Days will be converted into Months).

So if I have a date like 06.03.1992 (6th of march), the converter makes it the 3rd of June. And if the day is higher than 12, the program completely crashes.

I know that I can specify the date format somehow, but I haven't really got a working solution. Any ideas for my problem?

Thanks in advance, Daniel

Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
user3411789
  • 45
  • 1
  • 6
  • 1
    read http://james.newtonking.com/archive/2009/02/20/good-date-times-with-json-net and http://www.hanselman.com/blog/OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI.aspx – jparram Mar 12 '14 at 17:02
  • Dates don't have formats. Formats come into play when you serialize them to strings. There is no standard JSON date format, just conventions. The most popular is the one returned by Javascript's `Date.ToJSON`. None of the formats you mentioned matches this. Please post the code that exhibits the problem and the version of Json.Net you used. The default format [changed after Json.Net 4.5](http://james.newtonking.com/json/help/index.html?topic=html/DatesInJSON.htm) – Panagiotis Kanavos Mar 12 '14 at 17:03
  • 1
    @jparram that [isn't valid anymore](http://james.newtonking.com/json/help/index.html?topic=html/DatesInJSON.htm), after Json.NET 4.5 the ISO 8601 format is used by default, ie "2012-03-19T07:22Z" – Panagiotis Kanavos Mar 12 '14 at 17:05
  • @PanagiotisKanavos so it's the default before 4.5 and afterwards it's ISO 8601 – jparram Mar 12 '14 at 17:07

2 Answers2

1

You can control the format that Json.Net uses for serializing and deserializing dates by creating a new instance of the IsoDateTimeConverter class, setting the DateTimeFormat property on it as you require and then passing the converter to the serializer.

Here is a demo:

class Program
{
    static void Main(string[] args)
    {
        IsoDateTimeConverter dateConverter = new IsoDateTimeConverter
        {
            DateTimeFormat = "dd.MM.yyyy"
        };

        Foo foo = new Foo { Date = new DateTime(2014, 3, 12) };

        // serialize an object containing a date using the custom date format
        string json = JsonConvert.SerializeObject(foo, dateConverter);
        Console.WriteLine(json);

        // deserialize the JSON with the custom date format back into an object
        Foo foo2 = JsonConvert.DeserializeObject<Foo>(json, dateConverter);
        Console.WriteLine("Day = " + foo2.Date.Day);
        Console.WriteLine("Month = " + foo2.Date.Month);
        Console.WriteLine("Year = " + foo2.Date.Year);
    }
}

class Foo
{
    public DateTime Date { get; set; }
}

Output:

{"Date":"12.03.2014"}
Day = 12
Month = 3
Year = 2014

Note: with this approach, the date format will be applied to all dates on all objects you are serializing or deserializing. If you require different formats for different dates, then see this question which offers a couple of possible solutions.

Community
  • 1
  • 1
Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
0

Chances are pretty high that .net is serializing the date as follows:

\/Date(1293034567877)\/

You will need to parse that date and arrange it manually:

function ndateFormatter(cellval, opts, rwdat, _act) {
    var time = cellval.replace(/\/Date\(([0-9]*)\)\//, '$1');
    var date = new Date();
    date.setTime(time);
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var year = date.getFullYear();
    return ((month > 9 ? month : "0" + month) + "/" + (day > 9 ? day : "0" + day) + "/" + year);
};

or in your case the function would return:

return ((day > 9 ? day : "0" + day) + "." + (month > 9 ? month : "0" + month) + "." + year);
Rod Hartzell
  • 420
  • 3
  • 9