0

I'm having a javascript object as below.

var obj = { pageSize:"25",asOfDate:"Thu Sep 25 00:00:00 UTC+0530 2014"};

when i stringify it,

var d = JSON.stringify(obj);

its giving me result as

{"pageSize":"25","asOfDate":"2014-09-24T18:30:00Z"}

what could be the reason that its giving date 2014-09-24 than 2014-09-25 ?

EDIT:

My deployment server is located in US (Eastern Time UTC -5:00).when i check the site from my local machine in india its giving me date as 24 Sept 2014

RobertKing
  • 1,853
  • 8
  • 30
  • 54

3 Answers3

3

UTC+0530 declares a UTC time offset. Seems that 5:30 is around the India or Sri Lanka area.

"2014-09-24T18:30:00Z" is the same as "Thu Sep 25 00:00:00 UTC+0530 2014" in two different formats. The Z in the first format is resolving to UTC (GMT) time, which in this case is -5:30. So 18:30 is 24:00 - 5:30.

So, if you are stringify-ing in a timezone that is negative offset (say in the United States UTC−08:00) then it could push the date back by one day when parsing.

I think this is what you are seeing.

Related SO Question: JSON Stringify changes time of date because of UTC

Community
  • 1
  • 1
Davin Tryon
  • 66,517
  • 15
  • 143
  • 132
  • but i'm `stringify`-ing in timezone `5:30` still its giving date back by one day – RobertKing Sep 25 '14 at 14:00
  • `"2014-09-24T18:30:00Z"` is the same as `Thu Sep 25 00:00:00 UTC+0530 2014`. The `Z` indicates that it is UTC (GMT) time. – Davin Tryon Sep 25 '14 at 14:03
  • checkout the other SO question I refer to: http://stackoverflow.com/questions/1486476/json-stringify-changes-time-of-date-because-of-utc – Davin Tryon Sep 25 '14 at 14:07
  • The server could always pass UTC. However, that is probably not an option. Or pass a string from the server and then handle the parse yourself. – Davin Tryon Sep 25 '14 at 14:11
1

Try this

    var obj = { pageSize:"25",asOfDate:"Thu Sep 25 00:00:00 UTC+0530 2014"};
    obj.asOfDate = reverseUTC(obj.asOfDate);
    var d = JSON.stringify(obj);

    function reverseUTC(updatedDate) {
       if ($.isEmptyObject(updatedDate)) {
          var offset = updatedDate.getTimezoneOffset();

           var currentDateTime = new Date();
           updatedDate.setHours((currentDateTime.getHours() * 60 + currentDateTime.getMinutes() - offset) / 60);
           updatedDate.setMinutes((currentDateTime.getHours() * 60 + currentDateTime.getMinutes() - offset) % 60);
    return updatedDate;
        }
    }
Swarup
  • 13
  • 5
0

There is no standard format for passing dates in JSON, so JSON.stringify is just calling the default date.prototype.toString() method and that is taking the timezone into account.

You need to ensure that the date is converted into a string to your particular requirements and only convert to JSON format.

Alnitak
  • 334,560
  • 70
  • 407
  • 495