0

I am writing a section on my angular app to select some timezones (i don't want to use a library, as I don't really need all the possible zones but just the basic ones) and so I found a list of zones and I have created this scope object:

$scope.timezones = [
      {GMT: "-1200", label: "(GMT -12:00) Eniwetok, Kwajalein"},
      {GMT: "-1100", label: "(GMT -11:00) Midway Island, Samoa"},
      {GMT: "-1000", label: "(GMT -10:00) Hawaii"},
      {GMT: "-0900", label: "(GMT -9:00) Alaska"},
      {GMT: "-0800", label: "(GMT -8:00) Pacific Time (US & Canada)"},
      {GMT: "-0700", label: "(GMT -7:00) Mountain Time (US & Canada)"},
      {GMT: "-0600", label: "(GMT -6:00) Central Time (US & Canada), Mexico City"},
      {GMT: "-0500", label: "(GMT -5:00) Eastern Time (US & Canada), Bogota, Lima"},
      {GMT: "-0400", label: "(GMT -4:00) Atlantic Time (Canada), Caracas, La Paz"},
      {GMT: "-0350", label: "(GMT -3:30) Newfoundland"},
      {GMT: "-0300", label: "(GMT -3:00) Brazil, Buenos Aires, Georgetown"},
      {GMT: "-0200", label: "(GMT -2:00) Mid-Atlantic"},
      {GMT: "-0100", label: "(GMT -1:00 hour) Azores, Cape Verde Islands"},
      {GMT: "0000", label: "(GMT) Western Europe Time, London, Lisbon, Casablanca"},
      {GMT: "+0100", label: "(GMT +1:00 hour) Brussels, Copenhagen, Madrid, Paris"},
      {GMT: "+0200", label: "(GMT +2:00) Kaliningrad, South Africa"},
      {GMT: "+0300", label: "(GMT +3:00) Baghdad, Riyadh, Moscow, St. Petersburg"},
      {GMT: "+0350", label: "(GMT +3:30) Tehran"},
      {GMT: "+0400", label: "(GMT +4:00) Abu Dhabi, Muscat, Baku, Tbilisi"},
      {GMT: "+0450", label: "(GMT +4:30) Kabul"},
      {GMT: "+0500", label: "(GMT +5:00) Ekaterinburg, Islamabad, Karachi, Tashkent"},
      {GMT: "+0550", label: "(GMT +5:30) Bombay, Calcutta, Madras, New Delhi"},
      {GMT: "+0575", label: "(GMT +5:45) Kathmandu"},
      {GMT: "+0600", label: "(GMT +6:00) Almaty, Dhaka, Colombo"},
      {GMT: "+0700", label: "(GMT +7:00) Bangkok, Hanoi, Jakarta"},
      {GMT: "+0800", label: "(GMT +8:00) Beijing, Perth, Singapore, Hong Kong"},
      {GMT: "+0900", label: "(GMT +9:00) Tokyo, Seoul, Osaka, Sapporo, Yakutsk"},
      {GMT: "+0950", label: "(GMT +9:30) Adelaide, Darwin"},
      {GMT: "+1000", label: "(GMT +10:00) Eastern Australia, Guam, Vladivostok"},
      {GMT: "+1100", label: "(GMT +11:00) Magadan, Solomon Islands, New Caledonia"},
      {GMT: "+1200", label: "(GMT +12:00) Auckland, Wellington, Fiji, Kamchatka"}
    ]

then, using the date object I get back from an api call, I am stripping the relevant parts using momentjs.format().

The date object I am using for this example is the following:

var dateTime = "Tue Apr 25 2017 15:25:05 GMT+0100 (GMT Daylight Time)"

and then I create more scope variables like this:

$scope.Time = moment(dateTime).format("HH:mm")
$scope.Date = moment(dateTime).format("D/MM/YYYY")
$scope.Timezone = getTimezoneLabel(moment(dateTime).format("ZZ"))

getTimezoneLabel = function(timezone) {
  var result;
  result = null;
  angular.forEach($scope.timezones, function(t) {
    if (t.GMT === timezone) {
      return result = t.label;
    }
  });
  return result;
};

and the result on the page is pretty much all correct, apart from the timezone. The timezone that comes back is the following:

(GMT +1:00 hour) Brussels, Copenhagen, Madrid, Paris

but it's wrong! the correct one should be london, but the daylight saving times interferes with this.

So, my ideas where to get the value in brackets using moment.format({I couldn't find a right format for this}).

Is it even possible? do you have a better suggestion? thanks

Nick
  • 13,493
  • 8
  • 51
  • 98
  • Am I missing something here? You give the Brussles time, but why do you expect to get London as timezone? – AidaNow Apr 25 '17 at 16:11
  • If i got it right, when there's daylight saving tine,, they are changin the time to be +1, meaning that London becames +1 and bruxells +2 and so on – Nick Apr 25 '17 at 16:16
  • 1
    Sorry, but your entire approach is flawed. Please read [the timezone tag wiki](http://stackoverflow.com/tags/timezone/info), especially the sections "Time Zone != Offset", and "Time Zone Databases". After reading, let me know if you want me to elaborate in a detailed answer here. – Matt Johnson-Pint Apr 25 '17 at 16:18
  • That's just a response from someone else...i was wonderibg if there was a fix – Nick Apr 25 '17 at 16:20
  • 1
    Hi Nick. I help maintain moment and moment-timezone, and the tag wiki I linked to, so it's actually *my* response. You might also want to read [these best practices](http://stackoverflow.com/a/2532962/634824) which I've helped to curate. There's no "fix" because your code represents multiple logical fallacies about how time zones actually work. The only answer I could write on this would be to tear apart your code piece by piece to point out the flaws - which I don't think is what you're looking for. :) – Matt Johnson-Pint Apr 25 '17 at 16:24

0 Answers0