Convert DateTime To JSON DateTime

in client side you can use this function to show a right date to client(I use it on my projects):

function parseJsonDate(jsonDate) {
var offset = new Date().getTimezoneOffset() * 60000;
var parts = /\/Date\((-?\d+)([+-]\d{2})?(\d{2})?.*/.exec(jsonDate);
if (parts[2] == undefined) parts[2] = 0;
if (parts[3] == undefined) parts[3] = 0;
d = new Date(+parts[1] + offset + parts[2] * 3600000 + parts[3] * 60000);
date = d.getDate() + 1;
date = date < 10 ? "0" + date : date;
mon = d.getMonth() + 1;
mon = mon < 10 ? "0" + mon : mon;
year = d.getFullYear();
return (date + "." + mon + "." + year);
};

This function is return right date in format: dd.mm.yyyy, but you can change it if you need. I hope that I help you.


You could change your WS to return a long with the value of the DateTime. The value to return is the number of milliseconds since the Unix Epoch (01/01/1970). This could be done with an extension method on DateTime something like:

public static class DateTimeExtensions
{
    ...
    private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1);

    public static long ToUnixTime(this DateTime dateTime)
    {
        return (dateTime - UnixEpoch).Ticks / TimeSpan.TicksPerMillisecond;
    }
    ...
}

And your web service method might look something like:

public long GetMyDate(...)
{
    DateTime dateTime = ...;
    return dateTime.ToUnixTime();
}

with Json.NET :

string date = Newtonsoft.Json.JsonConvert.SerializeObject(DateTime.Now);