2

My controller sends date data to the view using JSON. On the view I'm sending those data to a jQuery template to display. My problem is date, since I'm getting this value like

"/Date(1245398693390)/"

So I've tried with this solution but using FireBug I can see that an error is thrown at line

var value = new Date(parseInt(jsonDate.substr(6)));

with the error

jsonDate.substr is not a function

If it's important at all I'm trying to implement this using jQuery mobile.

Update

<script id="myDataTemplate" type="text/html">          
    <li>@Html.ActionLink("${GetDateString(Date)}", "Fetch", "Data")</li>            
</script>

and this is js function injected in Layout view

<script>
            function getDateString(jsonDate) {
                if (jsonDate == undefined) {
                    return "";
                }
                var utcTime = parseInt(jsonDate.substr(6));

                var date = new Date(utcTime);
                var minutesOffset = date.getTimezoneOffset();

                return date.addMinutes(minutesOffset).toString("M/d/yyyy h:mm tt");
            }
        </script>

Update 2 This is js function I originally used

function GetDate(jsonDate) {
  var value = new Date(parseInt(jsonDate.substr(6))); //breaks
  return value.getMonth() + 1 + "/" + value.getDate() + "/" + value.getFullYear();
}
Community
  • 1
  • 1
user1765862
  • 13,635
  • 28
  • 115
  • 220
  • what you're getting back is actually javascript code, not json proper. what this means is that if you execute that code, it will generate a new `Date` object starting at the #milliseconds since unix epoch date. in other words, you need to treat that value as JSONP. – Kristian Mar 23 '14 at 16:47
  • @Kristian thanks for your time. Question is updated. Can you please add solution with concrete example. – user1765862 Mar 23 '14 at 17:08
  • so, when you console out the `jsonDate` var you get `"/Date(1245398693390)/"`? – Kristian Mar 23 '14 at 17:18
  • I've just posted originally used js functon. It breaks on var value = ... line. Ofcourse I've used @Html.ActionLink("${GetDate(Date)}"... – user1765862 Mar 23 '14 at 17:22
  • yes, that is because the value of jsonDate is not a proper string. thus the function `substr` is unavailable. therefore, in order to continue debugging this, you need to throw a `console.log` inside of that function just before the breaking line to identify exactly what the value of jsonDate is, and make sure that it is actually a string. – Kristian Mar 23 '14 at 17:27
  • Few solutions here ---- http://stackoverflow.com/questions/1016847/converting-net-datetime-to-json – Tasos Mar 23 '14 at 18:14
  • wow -- /Date(1245398693390)/ its the exact value as the one in the 2009 post – Tasos Mar 23 '14 at 18:21
  • yep, copied that value when try to test. So any concrete solution? – user1765862 Mar 23 '14 at 18:23
  • did you see this solution --- var milli = "/Date(1245398693390)/".replace(/\/Date\((-?\d+)\)\//, '$1'); var d = new Date(parseInt(milli)); – Tasos Mar 23 '14 at 18:32
  • the above didn't work in the console --- this does though --- var aa = "/Date(1245398693390/".match(/\d+/); ---- var bb = parseInt(aa); ---- var date = new Date(bb); --- alert(date); ---- try that – Tasos Mar 23 '14 at 20:04

1 Answers1

1

Working test

   var aa = "/Date(1245398693390/".match(/\d+/); 

   var bb = parseInt(aa);

   var date = new Date(bb);

   alert(date);

enter image description here

Tasos
  • 5,321
  • 1
  • 15
  • 26