Convert DateTime To JSON DateTime
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!
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, 2022Comments
-
manoj almost 2 years
I have a WebService which return DateTime Field.
I get a result
/Date(1379048144000)/
buti 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 over 10 yearsi 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 over 10 yearsthen where u want to parse? javascript? server side?
-
manoj over 10 yearsi want to return only
1379048144000
by serverside c# . -
jpblancoder over 9 yearsI believe that you should change: date = d.getDate() + 1; To: date = d.getDate();
-
Gurpreet Singh over 7 yearsi was getting date as : "\/Date(1476775277500)\/". This solution worked for me as well , thank you ! , able to convert to proper js date
-
Siavash Mortazavi about 5 yearsHere 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/Data/…