問題描述
我正在使用帶有時區的 moment.js 在 web 應用程序中進行日期操作.我將所有日期存儲在 UTC 中,并將它們作為 UTC 返回給 UI.我有用戶的時區.我想將 UTC 時間轉換為本地用戶時區.
i'm using moment.js with timezones for date manipulation in a webapp. i'm storing all the dates in UTC and return them to UI as UTC. I'm having the timezone of the user. i want to convert the UTC time to the local Users time zone.
var timezone = "America/New_York";
var utcDate = "2013-10-16T10:31:59.0537721Z";
var localDate = moment(utcDate).utc().tz(timezone).format()
當我嘗試這樣做時,我的時間不對.不知道如何讓它與時刻一起工作
When i try to do this i'm getting wrong time. not sure how to get this working with moment
推薦答案
你可以試試:
moment.utc(utcDate).tz(timezone).format()
但這應該沒關系.它們都應該產生:"2013-10-16T06:31:59-04:00"
.
But it shouldn't matter. They should both produce: "2013-10-16T06:31:59-04:00"
.
它適用于我,在 Chrome 30 上運行,所以它可能與瀏覽器相關.
It works for me, running on Chrome 30, so it's probably browser related.
如果您在 IE8 上運行 Moment.js 2.3.1 或更早版本,那么這是問題 #1175,在 2.4.0 中修復.更新到最新版本應該可以解決問題.
If you're running Moment.js 2.3.1 or earlier on IE8, then it's a side effect of issue #1175, which was fixed in 2.4.0. Updating to the latest version should solve the problem.
這篇關于使用 moment.js 將 Utc 日期更改為本地日期的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!