2016-04-29 38 views
0

如何在UTC中重新設置時間標記和相同類中的ISO 8601時間戳,並通過JavaScript調整到用戶區域設置和時區?如何在UTC中重新設置時間標記和相同類中的ISO 8601時間戳,並通過JavaScript調整到用戶區域設置和時區?

我確實有一個html頁面,其中包含一個帶有多個時間戳的HTML頁面,採用ISO 8601格式,採用UTC時間標籤封裝在同一個類中。

我想將它們重新格式化爲用戶區域設置格式和時區更正。

我想出了下面的代碼。在提到的評論中,我需要在TODO中添加哪些內容?

<!--- table with timestamps in ISO 8601 format and UTC ---> 
<table> 
<tr><td>1: </td><td><time class="UTC_ISO_Date">2013-02-07T23:00:00.000Z</time></td></tr> 
<tr><td>2: </td><td><time class="UTC_ISO_Date">2015-06-09T21:30:00.000Z</time></td></tr> 
<tr><td>3: </td><td><time class="UTC_ISO_Date">2016-03-022T09:00:00.000Z</time></td></tr> 
</table> 

// get the timezone of the user out of the browser 
var timezone = jstz.determine(); 
var usertz = timezone.name(); 

// TODO: get the locale of the user out of the browser  
// iterate over classes="UTC_ISO_Date" with each() 
$('.UTC_ISO_Date').each(function(i, obj) {  
    // format the timestamps into the correspondent format of the user locale and adjust to the users timezone 
    var formattedTimestamp = moment(i); 
    // TODO: update the value in the current class 

}); 
+0

請添加一個示例,您想實現什麼。 –

+0

使用[moment.js](http://momentjs.com/)進行時間和日期操作。 –

+0

<腳本src =「https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.3/moment-timezone.js」> 我已包含Moment.js,但未顯示。< – Thorsten

回答

0

你並不需要了解此方案的用戶的時區。您不需要jsTimeZoneDetect或時區。您只需撥打:

moment('2013-02-07T23:00:00.000Z').format() 

可以傳遞你想要參數的format功能。有關詳細信息,請參閱時刻文檔。

此外,大多數現代瀏覽器都會使用Date對象執行此操作,但您對輸出格式的控制不夠。

new Date('2013-02-07T23:00:00.000Z').toString() 
相關問題