0

I have the following Javascript line of code:

var showTime = new Date( 'Oct 15, 2017 19:45:00' ).getTime(); // October 15, 2017 at 7:45 p.m. ET or 4:45 p.m. PT

I am presently in the ET time zone (GMT-5 or Americas/New York). So I set the showTime variable to ET. However, I want the showTime var to actually be reflective of a Pacific Coast time which is 3 hours earlier -- in this case 4:45 p.m. Pac Time. And I want the time to be fixed and recognized globally and to not be offset according to local time zones.

Does the showTime variable get treated differently in all the various time zones around the world? If it gets treated according to the local machine's time zone, how do you lock the time to 10/15/17, 7:45 p.m. ET universally?

Thanks.

H. Ferrence
  • 7,906
  • 31
  • 98
  • 161
  • Can you explain what kind of things you want to show on the site? Example: eating at noon, there you want local time. Watching a live olympics event, there you want One moment in global time whatever is on the watch of the client... – Emmanuel Delay Sep 21 '17 at 13:26
  • I am using it as a countdown clock. I want the count down to reflect a count down to a fixed time at GMT-8 / Americas/Los Angeles -- no matter where the local client machine is located. – H. Ferrence Sep 21 '17 at 13:31
  • Keep in mind `America/Los_Angeles` is only in -8 for part of the year. It's in -7 during daylight saving time. It's not fixed. – Matt Johnson-Pint Sep 21 '17 at 19:28
  • @H.Ferrence - also, it would be a lot easier to count down to a specific UTC time, such that no client-side conversions are required. Converting from a specific time zone to UTC on the server side is usually trivial. – Matt Johnson-Pint Sep 21 '17 at 23:23

2 Answers2

1

The JavaScript Date object's getTime() method returns the milliseconds since January 1, 1970 UTC. That's universal time, irrespective of the locale of the current user.

The only timezone-dependent part of your code is the instantiation of the Date object.

This uses the current user's timezone:

var showTime = new Date( 'Oct 15, 2017 19:45:00' ) // 1508114700000 - I'm in CDT currently.

Since you want the time in US Pacific time zone, you should add the time zone information to the date string (Oct 15 is still in Daylight time, so I'm using PDT instead of PST, which is GMT-7):

var showTime = new Date( 'Oct 15, 2017 19:45:00 GMT-0700' ) // 1508121900000

That string is very implementation-specific and may break across different browsers and operating systems. Newer browsers (not IE 8) supporting ECMAScript 5 have standardized around simplified ISO 8601 (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse#Browser_compatibility and https://msdn.microsoft.com/en-us/library/ff743760%28v=vs.94%29.aspx). That string would look like:

var showTime = new Date( '2017-10-15T19:45:00-07:00' ) // 1508121900000

The ISO 8601 string would be the best version to use, if your project can get away with only supporting ECMAScript 5 implementations (http://kangax.github.io/compat-table/es5/).

Jay Dansand
  • 789
  • 1
  • 9
  • 13
  • Cool, thank you @JayDansand !! – H. Ferrence Sep 21 '17 at 13:48
  • While the first part of your answer is correct, the approach in your second part is highly implementation specific, and not recommended. Keep in mind that time zone abbreviations are not standardized, and can be ambiguous. – Matt Johnson-Pint Sep 21 '17 at 19:25
  • You're right; in my rush this morning I just wanted to augment the OP's code in as understandable a way as possible. In theory simplified ISO 8601 should work, but it really is implementation specific: IE 8 in both Standards and Quirks mode, for instance, requires a legacy format. Maximum compatibility (from my testing) would be `Sun Oct 15 2017 19:45:00 GMT-0700` as given by the PHP date format `'D M d Y H:i:s \G\M\TO'` (see https://msdn.microsoft.com/en-us/library/ff743760%28v=vs.94%29.aspx). If sticking to modern browsers only, then definitely use ISO 8601. – Jay Dansand Sep 21 '17 at 21:02
0
moment.tz.guess()

you get the server timezone and pass your time

this is bower package "moment-timezone": "^0.5.7",

Ravi Teja
  • 649
  • 7
  • 17