-1

I'm in the process of building out a timeline featured for my web app and I am manipulating the elements added to the timeline by using:

var time_going = new Date(data2.time_going * 1000),
    time_going_hour = time_going.getHours(),
    time_going_minutes = time_going.getMinutes(),
    left_position = (time_going_hour * width_by_hour) + (time_going_minutes * width_by_minute);

The problem is that the left_position is different based on your current timezone because the new date function is returning different information. Is there anyway to force the time to always be consistent to New York time?

The data2.time_going is in Unix Timestamp and is set to 1368687333 for example.

Jason Biondo
  • 736
  • 5
  • 16
  • 34
  • Those posts seem to require that you know what timezone you are converting the time to. I'd want something that would autodetect the timezone and then adjust the time properly. – Jason Biondo May 11 '13 at 03:41
  • So you'd like to display New York time no matter where the user is? – Qantas 94 Heavy May 11 '13 at 03:51
  • Jason, I'm confused. In your post you say you want to force it to new york time. In your comment, you say you want it to detect the user's time zone. Which is it? – Matt Johnson-Pint May 11 '13 at 04:04

1 Answers1

2

As you probably already know, UNIX "Epoch" times have no timezone. This makes dealing with UNIX timestamps easy, because you never have to keep track of what timezone they are in.

Similarly, JavaScript Date objects are timezone independent. When you call getHours(), that method is doing the time-offset math for the timezone of the current machine.

So instead of always using New York time, you may better off doing all your time math using the UTC timezone. In other words, instead of using the locale-dependent getHours() and getMinutes(), try using getUTCHours() and getUTCMinutes(). So long as you are initializing your Date object with a UNIX Epoch time, those methods will always return the same value no matter the timezone of the machine they are running on.

Eric McCarthy
  • 301
  • 2
  • 7