2014-10-02 31 views
0

所以我想花時間,應用程序範圍內,並顯示在用戶的時區。這是我已經研究出來的,它正在工作。不過,我相信有更好的方法來做到這一點。起始時區將始終爲中央。中央= 5。我只關心美國的時區,但如果有一個簡單的方法來實現它在世界範圍內,我也是這樣。我正在使用的文本標籤將顯示爲下午4:00,這就是爲什麼有這麼多的子串。Javascript的轉換時區date

function timeZones() { 
    var timezone = new Date().getTimezoneOffset(); 
    timezone = timezone/60; 
    //STARTING TIMEZONE WILL ALWAYS BE CENTRAL 
    var difference = 5 - timezone; 
    $(".time-zone").each(function() { 
     var a = $(this).text(); 
     var hour = a.substring(0, a.indexOf(":") - 1); 
     hour = parseInt(a); 
     var yourTime; 
     //East Coast 
     if (difference == 1) { 
      hour = hour + difference; 
      if (hour == 12) { 
       yourTime = hour + a.substring(a.indexOf(":"), a.indexOf("A") - 1) + "PM"; 
      } 
      else if (hour > 12) { 
       hour = hour - 12; 
       yourTime = hour + a.substring(a.indexOf(":"), a.indexOf("A") - 1) + "PM"; 
      } 
      else { 
       yourTime = hour + a.substring(a.indexOf(":")); 
      } 
      $(this).text(yourTime); 
     } 
     //Mountain 
     if (difference == -1) { 
      hour = hour + difference; 
      if (hour == 0) { 
       hour = 12; 
       yourTime = hour + a.substring(a.indexOf(":")); 
      } 
      else if (hour < 0) { 
       hour = 12 + hour; 
       yourTime = hour + a.substring(a.indexOf(":"), a.indexOf("P") - 1) + "AM"; 
      } 
      else { 
       yourTime = hour + a.substring(a.indexOf(":")); 
      } 
      $(this).text(yourTime); 
     } 
     //West Coast 
     if (difference == -2) { 
      hour = hour + difference; 
      if (hour == 0) { 
       hour = 12; 
       yourTime = hour + a.substring(a.indexOf(":")); 
      } 
      else if (hour < 0) { 
       hour = 12 + hour; 
       yourTime = hour + a.substring(a.indexOf(":"), a.indexOf("P") - 1) + "AM"; 
      } 
      else { 
       yourTime = hour + a.substring(a.indexOf(":")); 
      } 
      $(this).text(yourTime); 
     } 
     //Alaska 
     if (difference == -3) { 
      hour = hour + difference; 
      if (hour == 0) { 
       hour = 12; 
       yourTime = hour + a.substring(a.indexOf(":")); 
      } 
      else if (hour < 0) { 
       hour = 12 + hour; 
       yourTime = hour + a.substring(a.indexOf(":"), a.indexOf("P") - 1) + "AM"; 
      } 
      else { 
       yourTime = hour + a.substring(a.indexOf(":")); 
      } 
      $(this).text(yourTime); 
     } 
    }); 
} 
+0

我想你應該看看時區時區庫,而不是試圖實現這一點。它會爲你節省很多頭痛。 – Tuan 2014-10-02 17:13:17

+0

那裏有那麼多。你會推薦哪一個? – 2014-10-02 17:21:46

+2

這是我正在談論的那個:http://momentjs.com/ – Tuan 2014-10-02 17:33:28

回答

1

你說:

開始時區始終是中央。中心= 5.

這是不正確的。一個時區和一個時區的偏移量是而不是是一樣的東西。美國中部時區在冬季使用UTC-6偏移量,並在daylight saving time生效時在夏季使用UTC-5偏移量。你不能硬編碼這些。另請參見the timezone tag wiki中的「時區!=偏移」。

如果您需要使用JavaScript中的非本地時區,則需要一個庫,例如我列出的那些庫here。例如,下面是如何使用moment-timezone來實現這個功能 - 這是moment.js庫的附加功能。

moment.tz('2014-10-02 01:23:00','America/Chicago').local().format('YYYY-MM-DD h:mm a') 

在上例中,解析了芝加哥(美國中部時間)的本地日期/時間。然後將其轉換爲用戶瀏覽器運行的任何時區(使用local函數)。然後根據指定的格式將其格式化爲一個字符串。

+0

3年後進入我的編程生涯中,我學會了總是使用Momentjs並停止嘗試重新設計車輪。說實話,這個老代碼的問題是,我當時在工作的公司沒有將他們的日期存儲爲UTC。 – 2017-10-25 17:23:13

0

東西我扔在一起

// timezone_out optional = local timezone 
function timeConversionGenerator(timezone_in, timezone_out) { 
    if (undefined === timezone_out) 
     timezone_out = new Date().getTimezoneOffset(); 
    var d = timezone_in - timezone_out; 
    return function (time) { 
     var hh = time.hh, 
      mm = time.mm + d, 
      ss = time.ss; 
     while (mm < 0) mm += 60, hh -= 1; 
     while (mm >= 60) mm -= 60, hh += 1; 
     while (hh < 0) hh += 24; 
     hh %= 24; 
     return { 
      hh: hh, 
      mm: mm, 
      ss: ss 
     }; 
    } 
} 

var c = timeConversionGenerator(5 * 60); // 5 hours behind UTC to local time 

現在

c({ 
    hh: 13, 
    mm: 18, 
    ss: 0 
}); 
/* converted to my local time (British Summer Time) 
{ 
    hh: 19, 
    mm: 18, 
    ss: 0 
} 
*/ 

然後

function toAMPMString(time) { 
    return ((time.hh % 12) || 12) 
     + ':' 
     + (time.mm < 10 ? '0' : '') + time.mm 
     + ' ' + (time.hh < 12 ? 'AM' : 'PM'); 
} 

// so 
toAMPMString(c({ hh: 13, mm: 18, ss: 0})); // "7:18 PM"