Search code examples
c#.netdatetimeclrstoredprocedure

Deserialize DateTime From C# CLR


I have a json string in my CLR class like this:

string parameteres = "{\"Parameter\":{\"personId\":\""+PersonId.ToString()+"\",\"date\":\""+Date.Value+"\"}}";

The CLR class calls The REST api and passes parameters to it. In my REST api I deserialize parameter like this:

JObject enterddata = JObject.Parse(jsoninput);
        string jsonparam = enterddata["Parameter"].ToString();
        var personId = new Guid();
        var date = new DateTime();
        try
        {
            JObject data = JObject.Parse(jsonparam);
            personId = new Guid(data["personId"].ToString());

            date = (DateTime)data["date"];

           //Other Codes Goes Here
        }
        catch (Exception ex)
        {

        }

and when I reach this line:

date = (DateTime)data["date"]; 

The Exception occurred and doesn't convert it to DateTime. How can I make it work?


Solution

  • You have to specify the correct date format in this case ISO should do it

    string parameteres = string.Format"{\"Parameter\":{\"personId\":\"{0}\",\"date\":\"{1:yyyy-MM-dd HH:mm:ss}\"}}",PersonId,Date.Value);