Last post May 26, 2017 05:13 PM by TonyR6
Member
102 Points
287 Posts
May 25, 2017 05:55 PM|tonyR6|LINK
Hi there guys, been trying to convert a json date that's been posted on one of the service(handler.ashx file)
i tried using one of the following methods below...
the date comes in one of this format "/Date(1495640387558)/"
i would like to save the date to sql database...
private DateTime ConvertJsonStringToDateTime(string jsonTime) { if (!string.IsNullOrEmpty(jsonTime) && jsonTime.IndexOf("Date") > -1) { string milliSec = jsonTime.Substring(jsonTime.IndexOf("(") + 1); string sign = milliSec.IndexOf("+") > -1 ? "+" : "/"; string hours = milliSec.Substring(milliSec.IndexOf(sign)); milliSec = milliSec.Substring(0, milliSec.IndexOf(sign)); hours = milliSec.Substring(0, milliSec.IndexOf(")")); return new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(Convert.ToInt64(milliSec)).AddHours(Convert.ToInt64(milliSec) / 100); } return DateTime.Now; } and public static DateTime ConvertJsonToDateTime(string microSec) { long milliSec = (long)(Convert.ToInt64(microSec)); DateTime startTime = new DateTime(1970, 1, 1); TimeSpan time = TimeSpan.FromMilliseconds(milliSec); DateTime result = new DateTime(time.Ticks + startTime.Ticks); return result; }
kind regards
Tony
All-Star
53751 Points
24069 Posts
May 25, 2017 07:14 PM|mgebhard|LINK
You're showing the result of the .NET serializer.
http://www.newtonsoft.com/json/help/html/DatesInJSON.htm
There is not need to go through all the trouble of parsing the string just deserialize the date or pass a DateTime type rather than a string.
May 25, 2017 08:01 PM|tonyR6|LINK
HI there mgebhard,
thanks for the reply...
okay i receive the date in json format and as a string
LastUpdated = "/Date(1495640387558)/";
so basically you saying i should pass it as datetime type? in one of the methods?
The link provided shows conversion from normal datetime to json format date
17652 Points
3510 Posts
May 26, 2017 02:43 AM|Chris Zhao|LINK
Hi Tony,
You could use Json.NET.
using Newtonsoft.Json; string sa = @"""" + "/Date(1495640387558)/" + @""""; DateTime dt = JsonConvert.DeserializeObject<DateTime>(sa);
Best Regards,
Chris
May 26, 2017 05:13 PM|tonyR6|LINK
Hi there Chris,
thanks for the informative reply, this is what i was looking for...
i must say it works perfectly
i was not able to install json.net library on the project because i'm already referencing
using Jayrock.Json; using Jayrock.JsonRpc; using Jayrock.JsonRpc.Web;
this is what i did based on your suggestion..
DateTime dt_lastUpdated = Jayrock.Json.Conversion.JsonConvert.Import<DateTime>(@"""" + lastUpdated + @"""");
thanks alot, hope this helps another person in future
Member
102 Points
287 Posts
How to convert Json Date to normal datetime posted from a webservice
May 25, 2017 05:55 PM|tonyR6|LINK
Hi there guys, been trying to convert a json date that's been posted on one of the service(handler.ashx file)
i tried using one of the following methods below...
the date comes in one of this format "/Date(1495640387558)/"
i would like to save the date to sql database...
kind regards
Tony
All-Star
53751 Points
24069 Posts
Re: How to convert Json Date to normal datetime posted from a webservice
May 25, 2017 07:14 PM|mgebhard|LINK
You're showing the result of the .NET serializer.
http://www.newtonsoft.com/json/help/html/DatesInJSON.htm
There is not need to go through all the trouble of parsing the string just deserialize the date or pass a DateTime type rather than a string.
Member
102 Points
287 Posts
Re: How to convert Json Date to normal datetime posted from a webservice
May 25, 2017 08:01 PM|tonyR6|LINK
HI there mgebhard,
thanks for the reply...
okay i receive the date in json format and as a string
LastUpdated = "/Date(1495640387558)/";
so basically you saying i should pass it as datetime type? in one of the methods?
The link provided shows conversion from normal datetime to json format date
kind regards
Tony
All-Star
17652 Points
3510 Posts
Re: How to convert Json Date to normal datetime posted from a webservice
May 26, 2017 02:43 AM|Chris Zhao|LINK
Hi Tony,
You could use Json.NET.
Best Regards,
Chris
Member
102 Points
287 Posts
Re: How to convert Json Date to normal datetime posted from a webservice
May 26, 2017 05:13 PM|tonyR6|LINK
Hi there Chris,
thanks for the informative reply, this is what i was looking for...
i must say it works perfectly
i was not able to install json.net library on the project because i'm already referencing
using Jayrock.Json;
using Jayrock.JsonRpc;
using Jayrock.JsonRpc.Web;
this is what i did based on your suggestion..
thanks alot, hope this helps another person in future
kind regards
Tony