开发者

Problem on JSON date Parsing

I have a JSON source that sends its data that I display on my web page. Problem is, the Date part that it is returning is some sort of a javascript object.

Please look at the sample response.

dteInstallDate
 date   16
 day    4
 hours   0
 minutes   0
 month   8
 nanos   0
 seconds   0
 time   1284566400000
 timezoneOffset -480
 year   110

When I try to print the object as console.log. It prin开发者_开发百科ts

data.resource_data.dteInstallDate.toString()
[object Object]

My problem is that I want to set this JSON response into my Jquery hidden input it updates the data into Object Object.

$("div#pmItems").find("#dteInstallDate").val(data.resource_data.dteInstallDate);

I am having thoughts on how to send Date object from Server Side into Javascript. Should I just convert it to string or send it as a long value. Which one do you think is better?

I have Spring MVC as my backend


You have a JSON source, correct? JSON is simply a notational standard for the purpose of cross-language communication. While a JSON object has the same notation as a shorthand-declared Javascript object, it is really just a representation of any object in any language. If you were to get a JSON feed that looked like this:

{
    "dteInstallDate":
    {
        "date"      : 16,
        "day"       : 4,
        "hours"     : 0,
        "month"     : 8,
        "nanos"     : 0,
        "seconds"   : 0,
        "time"      : 1284566400000,
        "timezoneOffset": -480,
        "year"      : 110
    }
}

In Javascript, all you'd have to do is call up the dteInstallDate.time property and put it into a new Date() object. A working example is here:

http://jsfiddle.net/kAxfY/

As you can see, the time is a little off what they say...I tried fooling around with the timezoneOffset to make it more accurate, but I couldn't. Typically, timezone offsets are done in minutes, so -480 would mean that it's -480/60 = -8 hours behind GMT. For more info on the Javascript Date object, see this page:

http://www.w3schools.com/jsref/jsref_obj_date.asp

Edit: and perhaps a bit more toward the point of your post...it doesn't matter how you send this data across to the browser, as long as enough information is there to create a Date object in Javascript. The four options for creating the date object are:

//no argument means it will create a date representing the local time on the machine on which the browser is running
var d = new Date();

//milliseconds
var d = new Date(1284566400000);

//a string
var d = new Date("October 13, 1975 11:13:00");

//giving precise values for each unit of time
var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜