Quantcast
Channel: WCF, ASMX and other Web Services
Viewing all articles
Browse latest Browse all 555

How to convert Json Date to normal datetime posted from a webservice

$
0
0

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


Viewing all articles
Browse latest Browse all 555

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>