1

This may be a possible duplicate, but I am posting this after searching a lot and trying various method.

I am passing model as a JsonResult in ajax success and binding the value to controls. In my model I have a datetime property which gets a proper date value but in my ajax success it gets converted to something like this '/Date(1493749800000)/' .

Now when I want to use the Datetime value for further function in Datetime property of my MVC model, it gets null.

Please do suggest a way I can handle this scenario.

Things I tried: Date.Parse(), Json.Parse(), string.Replace()

14
  • Is the 1493749800000 being sent from the browser to the server (i.e. generated by JS) or from the server to the browser (i.e. generated by C#)? Commented Jun 2, 2017 at 13:52
  • Please see this , this answers it'll help you Commented Jun 2, 2017 at 13:52
  • arent that ticks? Commented Jun 2, 2017 at 13:52
  • Other pre-existing answers: stackoverflow.com/questions/44330532/… Commented Jun 2, 2017 at 13:53
  • mobile.developer.com/net/… this should help you Commented Jun 2, 2017 at 13:55

4 Answers 4

0

IMHO, it is always better to work with timestamp dates when doing backend - frontend communication. That way you reduce chances of getting into problems on conversion. Here you have two functions to make that work from your api:

        public static DateTime UnixTimeStampToDateTime(double unixTimeStamp)
        {
            System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
            dtDateTime = dtDateTime.AddSeconds(unixTimeStamp).ToLocalTime();
            return dtDateTime;
        }

        public static long DateTimeToUnixTimeStamp(DateTime value)
        {
            long epoch = (value.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
            return epoch;
        }
Sign up to request clarification or add additional context in comments.

Comments

0

I think you can use below line to deserealize your serealize date something like this : JsonConvert.DeserializeObject<DateTime>(yourDate);

You can also deserealize you model by following way :

JsonConvert.DeserializeObject<YouModel>(JsonModelData);

1 Comment

along with datetime i do have other properties also that are passed in my model
0

This is how I did in one of my previous project, I assume that you got all your data including date in your ajax success method. Now,

success: function(data)
 {
   var jsonDate = data.myDateField;
   var value = new Date(parseInt(jsonDate.toString().substr(6)));
   var finalResult = value.getMonth() + 1 + "/" + value.getDate() + "/" + 
                     value.getFullYear();
 }

So finalResult is your Date that you are expecting. Now you can render that in your HTML, as simple.

For more info, you can visit this blog, written by great Hanselman.

Bellow is the snippet, taking your date coming from server. Just run it. And dont confuse with the string /Date(1493749800000)/ with the finalResult. I mean the date after conversion. Because this is the actual date that you saved in your database.

var jsonDate = "/Date(1493749800000)/";
var value = new Date(parseInt(jsonDate.toString().substr(6)));
var finalResult = value.getMonth() + 1 + "/" + value.getDate() + "/" + 
                  value.getFullYear();
console.log(finalResult);
//alert(finalResult);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Hope it helps. Cheers! :)

3 Comments

Thats the error: VM922:2 Uncaught TypeError: jsonDate.substr is not a function
Check my updated answer above. It's working fine. You need to add toString() there. Even if I don't use toString() still it's working fine. Cheers
@AdityaPewekar See my above answer. Added a snippet by taking your Date coming from server. If you are getting error in substr, check with substring() method. Cheers! If it solve your problem dont forget to mark as answer :)
0
var input = '/Date(464560200000)/'
var a = /\/Date\((\d*)\)\//.exec(input);
var d = new Date(+a[1]);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.