0

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

CSharpNewBee
  • 1,951
  • 6
  • 28
  • 64
  • Would it not be simpler to convert the date/time on the server when you get it out of the DB and return it as a formatted string? – Reinstate Monica Cellio Mar 03 '14 at 16:55
  • yeah,I've done that, but it is still being returned as such – CSharpNewBee Mar 03 '14 at 16:56
  • 1
    You must have done something wrong then because if you format the date/time correctly on the server and then return it as a string, that's what you'll get, not the value shown in the question. – Reinstate Monica Cellio Mar 03 '14 at 16:57
  • This thread might help http://stackoverflow.com/questions/4511705/how-to-parse-json-to-receive-a-date-object-in-javascript – Tony Stark Mar 03 '14 at 17:01
  • @Archer that's the way c#'s javascript formatter parses DateTime object. – Steve Mar 03 '14 at 17:06
  • @Steve - It's not a valid JavaScript date/time value, which is why I suggest he formats it on the server and passes a string. The whole issue would disappear immediately. Have you tried converting that value to a date/time? – Reinstate Monica Cellio Mar 03 '14 at 17:07
  • @Archer it is pretty obvious that he is using a javascript serializer, and the problem with that is you can't specify how it serializes it. If you want to turn it into a string you would have to make another public string getter and do it that way. But it is one duplicated info in the returned Data. So depending on how perfectionist you are, it is one way to go. – Steve Mar 03 '14 at 17:13
  • @CSharpNewBee The problem here is the value you are returning. Serializing the date you show above, in C# (using JavaScriptSerializer) returns `"\/Date(1393822800000)\/"`. You can convert that back to a date/time object in Javascript using `new Date(1393822800000)` and that shows the correct date. The value you are returning is wrong. – Reinstate Monica Cellio Mar 03 '14 at 17:14
  • @Steve - Please see the comment I just posted above. He doesn't need to return it as a date. He can format it as a string and have a string property in the object he is returning. That would be much simpler and would fix the problem immediately. – Reinstate Monica Cellio Mar 03 '14 at 17:15
  • @Archer um....yea it looks strange to me that he has a negative value as the Date(xxxx). wonder how he did it – Steve Mar 03 '14 at 17:18
  • @Steve He says the format is -6xxxx (the xxxx being the date), but it's just not the right value, regardless. Not sure what's going on here at all. – Reinstate Monica Cellio Mar 03 '14 at 17:20
  • @Steve et al, see edit above, whats going on here, is square eye syndrome, and working too many hours :-) – CSharpNewBee Mar 03 '14 at 17:51
  • This question appears to be off-topic because it is about programmer error, not a code error. – Reinstate Monica Cellio Mar 03 '14 at 17:54
  • @Archer, this is a two part question, i still need to know how to get it in dd mm yyyy format. – CSharpNewBee Mar 03 '14 at 17:59
  • That's a simple search and has been answered on SO before... http://stackoverflow.com/questions/1056728/where-can-i-find-documentation-on-formatting-a-date-in-javascript – Reinstate Monica Cellio Mar 03 '14 at 18:01
  • thats fine, but at the time, I didn't know right? You're off-topic seems a bit unjust. – CSharpNewBee Mar 03 '14 at 18:02

2 Answers2

1

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.

Steve
  • 11,696
  • 7
  • 43
  • 81
0

Maybe you could use something like this:

var str = (result.getMonth() + 1) + "-" + result.getDate() + "-" + result.getFullYear();
return str;
zgood
  • 12,181
  • 2
  • 25
  • 26