Deserialize JSON Data automatically when using timestamp

355 Views Asked by At

I need to bring javascript timestamp to server in datetime format. My javascript object contains a property with datetimstamp ex : {"fromDate" : "new Date(1427826600000)"}

But the model on the server side has fromDate as DateTime. I need to deserialize it to DateTime on server side as I'm using different cultures.

below is the code.

Ajax Call

$.ajax({
    type: "POST",
    url: "Report/Update",
    dataType: "json",
    contentType: 'application/json',
    data: JSON.stringify(model),

Model

public class Report
{
 [JsonConverter(typeof (JavaScriptDateTimeConverter))]
 public DateTime FromDate { get; set; }

 [JsonConverter(typeof (JavaScriptDateTimeConverter))]
 public DateTime ToDate { get; set; }
}

Controller

[HttpPost]
public JsonResult UpdateReport(Report data)
{
        ...
}

I'm unable to do this can you help me out?

1

There are 1 best solutions below

8
On

Provided that aMs contains JS date, use this code to convert it to C# DateTime:

new DateTime(aMs * 10000 + 621355968000000000L);