Generating a date in a particular timezone [duplicate]

I have a timezone string, for example, "Pacific Time (US & Canada)". I need to get the equivalent of 3pm in that timezone in UTC for particular dates, including daylight savings time. How can I go about this?

For example, I need to find 3pm on December 3rd, PDT in UTC time.

Answers:

Answer

That time zone identifier looks like it came from Rails, or from Twitter (which uses Rails). You first need to convert it to a standard IANA zone identifer such as "America/Los_Angeles". See the MAPPING declaration in the ActiveSupport::TimeZone docs.

Then you can use one of the libraries I mentioned here.

In particular, you may want to try moment-timezone. You'll need the latest develop version for this particular feature, as it was added in issue #25 which is not yet in the current release. Then you can do something like this:

moment.tz("2013-12-03T15:00:00","America/Los_Angeles").utc().format()

You can adjust the input/output format to whatever makes sense for you and is supported by moment.js.

The other time zone libraries I mentioned may also offer similar features without as much overhead, so they are worth a look.

Answer

This seems to work for me though it does feel a little fragile

new Date('2013-12-03 15:00:00 PDT').toUTCString()

    -> "Tue, 03 Dec 2013 22:00:00 GMT"

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.