Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
140
rated 0 times [  145] [ 5]  / answers: 1 / hits: 153978  / 15 Years ago, mon, september 28, 2009, 12:00:00

My date objects 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 it's not, hence it should be



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


Basically I use this:



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 anyone help me?


More From » json

 Answers
52

Recently I have run into the same issue. And it was resolved using the following code:



x = new Date();
let hoursDiff = x.getHours() - x.getTimezoneOffset() / 60;
let minutesDiff = (x.getHours() - x.getTimezoneOffset()) % 60;
x.setHours(hoursDiff);
x.setMinutes(minutesDiff);

[#98609] Wednesday, September 23, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
luna

Total Points: 698
Total Questions: 114
Total Answers: 93

Location: Israel
Member since Wed, Apr 14, 2021
3 Years ago
;