Momentjs和时区 - 白天时间的小问题

时间:2017-04-25 14:36:55

标签: javascript angularjs timezone momentjs

我正在写一个关于我的角度应用程序的部分来选择一些时区(我不想使用图书馆,因为我不是真的需要所有可能的区域而只是基本的区域)所以我找到了一个区域列表,我创建了这个范围对象:

$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"}
    ]

然后,使用我从api调用中返回的日期对象,我使用momentjs.format()剥离相关部分。

我在本例中使用的日期对象如下:

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

然后我创建了更多这样的范围变量:

$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;
};

除了时区之外,页面上的结果几乎都是正确的。返回的时区如下:

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

但是错了!正确的应该是伦敦,但夏令时会干扰这一点。

所以,我的想法是使用moment.format({我无法为此找到正确的格式}}在括号中获取值。)

甚至可能吗?你有更好的建议吗?感谢

0 个答案:

没有答案