0

Our application is built using Java where we get Server time using datetime object. But when used from javascript I can't use the value as it is because value sent for UI is in string format (I don't know whether JSON attribute value retains date type in ajax when used from UI) and I want to use that value for pointstart of Highcharts and it expects the value to be of type Number.

So when I get value from REST, date will be in json format and type will be of type string. If it is converted to Date object, then it is converted back to Client's machine.

I have seen many suggestions and one among those is adding offset for the local time and I picked this for solving my problem(refer Convert date in one timezone to another timezone using timezone abbreviations for details on the solution). But in this at last it is converted to local string which is again not considered from HighCharts for pointstart. If I remove toLocaleString from Date object and try, the value what is receive is as below "Tue Oct 20 2015 23:05:37 GMT+0530 (IST)" where Date value Tue Oct 20 2015 23:05:37 (server time) is of PDT time but time zone value appended is "GMT+0530 (IST)" (client time zone). Value is correct but can't consider as client time zone is appended. What's wrong in this?

please let me know

Code snippet:

  // create Date object for current location
 var date = new Date();

// convert to msec
// add local time zone offset 
// get UTC time in msec
var utc = date.getTime() + (date.getTimezoneOffset() * 60000);

// create new Date object for different city
// using supplied offset
var newDate = new Date(utc + (3600000 * offset));

// return time as a string. But im removing this since i dont want of type string
return "The local time in " + city + " is " + newDate.toLocaleString();
Community
  • 1
  • 1
  • The JSON date should be ISO 8601 UTC, which should convert conveniently at the client using a simple parse function. Unfortunately browsers aren't sufficiently reliable to parse it with the built–in *Date.parse*. There are many answers here on how to parse an ISO 8601 string, see [*How to assume local time zone when parsing ISO 8601 date string?*](http://stackoverflow.com/questions/15517024/how-to-assume-local-time-zone-when-parsing-iso-8601-date-string) – RobG Oct 21 '15 at 07:09
  • You can also use a moment.js which apply timezones. http://api.highcharts.com/highcharts#global.getTimezoneOffset – Sebastian Bochan Oct 21 '15 at 07:33
  • Thanks RobG. Have one more problem with this. I'm able to get ISO 8601 date string from REST, but that is of format "2015-10-24T23:00:00.000-07:00". In the thread you mentioned above, the expected format is "2013-03-10T02:00:00Z". This is how i have written the code. new DateTime(value).toString(ISODateTimeFormat.dateTime()). value here is in long. Is there a way to convert to the specified format? i tried using SimpleDateFormat, but that is not accepted. – user3811156 Oct 26 '15 at 06:46

0 Answers0