0

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.

Phil
  • 157,677
  • 23
  • 242
  • 245
Dan Palumbo
  • 105
  • 1
  • 7
  • 1
    Does _Date_ recognise the timezone? e.g. for me `new Date('2013-10-15 00:25:00 PST').toUTCString(); // "Tue, 15 Oct 2013 08:25:00 GMT"` – Paul S. Oct 14 '13 at 23:26

2 Answers2

2

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.

Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
1

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"
Phil
  • 157,677
  • 23
  • 242
  • 245
  • 1
    Only a small handful of timezone abbreviations are recognized by this method. See [RFC 822](http://www.ietf.org/rfc/rfc0822.txt) section 5.1. In general, time zone abbreviations are not useful because there is too much ambiguity. For example, is "CST" short for "Central Standard Time" or "China Standard Time"? – Matt Johnson-Pint Oct 14 '13 at 23:40
  • This will work, though it would be nice if I could do it from the full fledged timezone string and not the abbreviation. – Dan Palumbo Oct 14 '13 at 23:44
  • @MattJohnson Another option is to replace the abbreviation with the offset in `±hhmm` format e.g. `-0700` or `-0800`. – Paul S. Oct 14 '13 at 23:45
  • 1
    @PaulS. - Absolutely. If you know the offset already, then half the battle is over and there is no need for the actual timezone in either abbreviated or IANA form. What you can't do is start replacing abbreviations with specific offsets. There are just too many overlaps and conflicts. – Matt Johnson-Pint Oct 14 '13 at 23:48
  • See [this list](http://en.wikipedia.org/wiki/Time_zone_abbreviations) for a better idea of all of the possible time zone abbreviations. – Matt Johnson-Pint Oct 14 '13 at 23:49