0

I have pcap import and read facility in my project. I have handling timezone in my project. Imported pcap is not read/display if I changed my timezone from GMT to other Asia/Kolkota.

How can I handle timezone issue in javascript. I am storing the value into the database is 2013-05-07 00:04:23.435751-06.

it should be handled in all timezone. thanks in advance

user1621860
  • 41
  • 2
  • 8

1 Answers1

0

I wish you are looking for this beautiful documentation. Thanks.

Update try this:

  var str= '2013-05-07 00:04:23.435751-06';
  var n = str.slice( -3 );
  var time = str.replace(" ","T");
  time = time.slice(0, -3);
  alert(calcTime(time, n));

  function calcTime(time, offset) {

  // create Date object for current location
  d = new Date(Date.parse(time));

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

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

  // return time as a string
  return "The Time is " + nd.toLocaleString();

  }
rony36
  • 3,277
  • 1
  • 30
  • 42
  • Thank you rony, It is helpful me but it is not working for the time - 2013-05-07 00:04:23.435751-06 which i used for timezone conversion of this value. Another thing how can I get machine offset to use in above example. I stored first packet time in database inthe form of ex. - 2013-05-07 00:04:23.435751-06 and then in UI it get converted using Date.parse and other js function for timezone. but it isnot workable for different timezone. – user1621860 May 07 '13 at 10:35
  • ok, in this time string `2013-05-07 00:04:23.435751-06`, `-06` is the offset. So, extract this form this string. And replacing this "white-space" with "T" and removing `-06` from the time string make a string like this: `2013-05-07T00:04:23.435751` and then this code will be worked `d = new Date(Date.parse('2013-05-07T00:04:23.435751'))` – rony36 May 07 '13 at 10:57
  • had updated my answer. Try it now. :) – rony36 May 07 '13 at 11:13
  • Thank you rony. I see it is working fine now but I need to do more debug on it. thanks a ton for your help to sort this one. – user1621860 May 07 '13 at 13:33
  • Rony - Previous problem is fixed but now issue with current timezone - suppose for the above pcap file I imported it from UTC+5.5 timezone and by your code I will convert it in local timezone and see the data but when I am in UTC timezone and access (above) already imported file, in that case I cannot see/read the data because in database the entry for both timezone is same and when I changed my timezone it will get change the local timezone but database value of first packet remain same. Please advice – user1621860 May 07 '13 at 13:51
  • eek! er.. um.. NO. This code is no good. Javascript local dates will always impose the local time zone rules. Javascript is flawed in this regard. You have added/removed some time to account for the offset, but that final `toLocaleString` will re-adjust it based on the LOCAL daylight savings rules. Different time zones around the world start and stop DST at very different times. Doing it like this is dangerous. In other words, you may still see the date an hour off depending on what values you use and what time zone you are in! – Matt Johnson-Pint May 07 '13 at 14:02
  • You need a TZDB implementation to do this in pure javascript, and it must be smart enough to work around the ugliness that is JS dates. Reference the dup post from the question comments. – Matt Johnson-Pint May 07 '13 at 14:07