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()

Aditya Pewekar
  • 91
  • 3
  • 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#)? – mjwills Jun 02 '17 at 13:52
  • Please see [this](https://stackoverflow.com/a/726869/2534646) , [this](https://stackoverflow.com/a/42662967/2534646) answers it'll help you – Curiousdev Jun 02 '17 at 13:52
  • arent that ticks? – pitersmx Jun 02 '17 at 13:52
  • Other pre-existing answers: https://stackoverflow.com/questions/44330532/json-date-to-c-sharp-mvc-date – Tomwa Jun 02 '17 at 13:53
  • http://mobile.developer.com/net/dealing-with-json-dates-in-asp.net-mvc.html this should help you – Chetan Jun 02 '17 at 13:55
  • @mjwills its geting fetched from database with datatype as Date – Aditya Pewekar Jun 02 '17 at 13:55
  • @Curiousdev both didnt worked for be ... subStr game me error not a function – Aditya Pewekar Jun 02 '17 at 13:57
  • @ChetanRanpariya tried that didn't worked – Aditya Pewekar Jun 02 '17 at 13:58
  • Does https://stackoverflow.com/a/36943282/34092 work? – mjwills Jun 02 '17 at 14:00
  • What are you trying to do with the date value in java script? – Chetan Jun 02 '17 at 14:00
  • @ChetanRanpariya I want to pass the date to my sp so that it can sort the result based on it – Aditya Pewekar Jun 02 '17 at 14:02
  • Can you provide more details about what you do with the value in the JavaScript and how are you sending that value to the stored procedure for sorting? For sorting you need column name not the value of it. May be I am missing something. Basically what you need is ISODateFormatter to be injected when you are serializing object to JSON. – Chetan Jun 02 '17 at 14:07
  • @ChetanRanpariya I have a form in which I have various control including datepicker. so when i click submit, all is passed to Contoller using ajax call in json format When I fetch the record I return the data in JsonResult to ajax success and then bind it to the controll . Here is where the data for Date is converted to that format. Now my requirement is to pass that date to my SP and base on that date fetch the records – Aditya Pewekar Jun 02 '17 at 14:38
  • One solution i found which works for me is var reviewDate='/Date(1493749800000)/' ; reviewDate=moment(reviewDate).format("YYYY/MM/DD"); If any other efficient method and correct way to implement please do suggest one. Thanks – Aditya Pewekar Jun 02 '17 at 14:40

4 Answers4

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;
        }
NicoRiff
  • 4,803
  • 3
  • 25
  • 54
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! :)

Basanta Matia
  • 1,504
  • 3
  • 15
  • 27
  • Thats the error: VM922:2 Uncaught TypeError: jsonDate.substr is not a function – Aditya Pewekar Jun 02 '17 at 14:44
  • 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 – Basanta Matia Jun 02 '17 at 14:53
  • @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 :) – Basanta Matia Jun 03 '17 at 07:20
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);

Nikunj Patel
  • 249
  • 3
  • 13
0
var input = '/Date(464560200000)/'
var a = /\/Date\((\d*)\)\//.exec(input);
var d = new Date(+a[1]);
Ahmad Behjati
  • 536
  • 6
  • 4