views:

216

answers:

4

Hi,

can anyone help, my date object in javascript are always represented by UTC +2 because of where i am located. hence like this

       Mon Sep 28 10:00:00 UTC+0200 2009

Problem is doing a Json.stringify converts the above date to

       2009-09-28T08:00:00Z  (notice 2 hours missing i.e. 8 instead of 10)

What i need is for the date and time to be honoured but its not, hence it should be

       2009-09-28T10:00:00Z  (this is how it should be)

Basically i using this i.e

       var jsonData = JSON.stringify(jsonObject);

I tried passing a replacer parameter (second parameter on stringify) but the problem is that the value has already been processed.

I also tried using toString() and toUTCString() on the date object, but these don't give me what i want either..

Can anyon help me?

A: 

Have the same problem and fix it by following way:

x = new Date();
x.setHours(x.getHours() - x.getTimezoneOffset() / 60);
Anatoliy
yes but this is if the website is used within my country, if its used in another country like USA - it wouldn't be 2 ...
mark smith
Obviously, this value should be calculated.
Anatoliy
thanks... I actually found a great library here, http://blog.stevenlevithan.com/archives/date-time-formatall you need to do this (maybe it will help you) , you pass false and it doesn't convert.var something = dateFormat(myStartDate, "isoDateTime", false);
mark smith
this is incorrect as it makes your code non-timezone safe -- you should be correcting the timezone when your read the date back in.
olliej
Timezone corrected by last upd.
Anatoliy
A: 

Usually you want dates to be presented to each user in his own local time-

that is why we use GMT (UTC).

Use Date.parse(jsondatestring) to get the local time string,

unless you want your local time shown to each visitor.

In that case, use Anatoly's method.

kennebec
+1  A: 

JSON uses the Date.prototype.toISOString function which does not represent local time -- it represents time in unmodified UTC -- if you look at your date output you can see you're at UTC+2 hours, which is why the JSON string changes by two hours, but if this allows the same time to be represented correctly across multiple time zones.

olliej
A: 

Just for the record, remember that the last "Z" in "2009-09-28T08:00:00Z" means that the time is indeed in UTC.

See http://en.wikipedia.org/wiki/ISO%5F8601 for details.

Locoluis