سؤال

I have a date in my DB of 2014-03-03 05:00:00, which is being rendered in JSON as:

enter image description here

the date is /Date{(-6xxxxx)/ and I call this method to parse it:

   function parseJsonDate(dateString) {
      var result = new Date(+dateString.replace(/\/Date\((-?\d+)\)\//gi, "$1"));
      var result = new Date(parseInt(dateString.replace('/Date(', '')));
      result.format("dd-MM-yyyy");
      return result;
   }

when running, i comment out one of the results lines, but get the same result for both:

enter image description here

the method is being called from Jquery template like such:

<tr>
   <td>
    <span id="approvedDate"><i class="glyphicon glyphicon-time" data-toggle="tooltip" 
        data-original-title="approved date"></i> ${parseJsonDate(AuditDate)}</span>
   </td>
</tr>

EDIT

What a muppet.. I spent so long thinking this was a JSON vconversion issue, i totally forgot to go back and check my dapper code. my ApprovalHistory objec had AuditDate, but I was asking for EnteredDate in the sql. So, it was doing as expected.

aaaaaaaarrr :-)

هل كانت مفيدة؟

المحلول

I am seeing something fishy there

var result = new Date(+dateString.replace(/\/Date\((-?\d+)\)\//gi, "$1"));
var result = new Date(parseInt(dateString.replace('/Date(', '')));
  1. you are making two variables named result within the same closure, is it intentional?
  2. what does result.format do? since result is a Date object I wouldn't assume that it would change the original type from Date to string.

Maybe

var s = result.format("dd-MM-yyyy"); return s;

is what you really want to do?

you can do this after the ajax complete, this will save you tons of trouble having to parse the Date(xxxxx) thing over and over again

data = data.replace(/\"\\\/Date\((-?\d+)\)\\\/\"/g, '$1')

this will convert "Date(xxxx)" to xxxx and then you can just call new Date(xxxx) to make new Date object.

نصائح أخرى

Maybe you could use something like this:

var str = (result.getMonth() + 1) + "-" + result.getDate() + "-" + result.getFullYear();
return str;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top