Convert DateTime To JSON DateTime

43,262

Solution 1

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();
}

Solution 2

with Json.NET :

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

Solution 3

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.

Solution 4

U can always solve your problem when sending a date in a JSON object to JS by converting the date as follows:

var myJSDate = (new Date(parseInt(obj.MyDate.substr(6)))).toJSON();

Where obj.Date contains the date you wanna format.

Then u'll get something like this: "2013-10-25T18:04:17.289Z"

U can always check it in Chrome console by writing:

(new Date()).toJSON();

Hope this helps!

Share:
43,262
manoj
Author by

manoj

Software Development is my biggest passion. I am always looking for something new to learn, for a new way to improve, for the next challenge. I always enjoy to analyze various software and application around the world and go deep into that how they work and just thinking about how they make this successful psychologically and technically. So i am Great fan of github.com like website. You can find my portfolio in linkedin and my some project on Github

Updated on July 09, 2022

Comments

  • manoj
    manoj almost 2 years

    I have a WebService which return DateTime Field.

    I get a result /Date(1379048144000)/ but

    i want just 1379048144000 how can i achieve that.

    [WebMethod]
    public DateTime GetServerDate()
    {
        return DateTime.Now;
    }
    

    by setting Header Content-Type: application/json; charset=utf-8; i got a result like /Date(1379048144000)/.

  • manoj
    manoj over 10 years
    i am returning DateTime and that return as /Date(1379048144000)/ i have no '/Date(1379048144000)/' within that c# code it is only respons whilwe calling by json post
  • Zaheer Ahmed
    Zaheer Ahmed over 10 years
    then where u want to parse? javascript? server side?
  • manoj
    manoj over 10 years
    i want to return only 1379048144000 by serverside c# .
  • jpblancoder
    jpblancoder over 9 years
    I believe that you should change: date = d.getDate() + 1; To: date = d.getDate();
  • Gurpreet Singh
    Gurpreet Singh over 7 years
    i was getting date as : "\/Date(1476775277500)\/". This solution worked for me as well , thank you ! , able to convert to proper js date
  • Siavash Mortazavi
    Siavash Mortazavi about 5 years
    Here is how .NET source code has set the epoch time: new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc), which might be more accurate. Ref: referencesource.microsoft.com/#System.Data.Services/System/D‌​ata/…