简体   繁体   中英

How to serialize Java Calendar and Java Date to Javascript Date

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM