1

I want to serialize from Java to JSON objects Java Calendar and Java Date to Javascript Date.

I want that in Javascript, the variable Javascript Date without having to do the "eval" or "new Date" has a Date value.

I tried to return "new Date (timeInMiliseconds)" but I obtained, obviously a 'String'I. I know I can do "eval" to this "String" but I wan't do it. I have also tried to return "miliseconds" and in "Javascript" call "new Date ()", But I do not want to do that.

The JSON currently looks like

 {hours:[
   {date:'new Date(1)', color:'fff'},
   {date:'new Date(2)', color: 'ddd'}, 
   ... ]}

Any suggestions?

Info: I'm using Spring MVC and Jackson to serialize.


Edit#1:

I know this methods:

Java Return ----------------------- Javascript

"new Date(100000)" ------------- var myDate = eval("new Date(100000)");

100000 ------------------------------ var myDate = new Date(10000);

But I'm searching the format that indicate to the client side parsers that the data fragment that was sent is a date representation. But how can you convert it to a JavaScript Date object?

11
  • 1
    What was the problem with new Date (timeInMiliseconds) ? Commented Jun 3, 2013 at 16:36
  • @mplungjan The problem is that Java return String, and I don't like do eval. Commented Jun 4, 2013 at 6:29
  • @mplungjan Ok. If I try that Java return for each var date is "new Date(miliseconds)". In Javascript, I need do "eval" each String var for get Javscript Date. Commented Jun 5, 2013 at 8:54
  • @mplungjan The output is from Server Controller to a jQuery plugin that read the json. Commented Jun 7, 2013 at 7:52
  • And how does the JSON look exactly? Commented Jun 7, 2013 at 9:46

1 Answer 1

2

The answer is no.

You cannot parse a string with date: "new date(123434534)" without eval or new Date(extracted timestamp) like this

object.date=new Date(object.date.replace(/[^\d]/g,""));

I still fail to see why you need to pass a string of "new Date(timestamp)"

You are better off and will save data if you just send the timestamp.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm passing a string of "new Date(timestamp)" for trying found a solution. I know that is it a bad idea and thet best solution is return time in milis. So the problem is that I would like that in the client side have a date without do "eval" or "new Date"
As I answered: Not possible

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.