問題描述
我有這個后端,它會在設(shè)定的時區(qū)向我發(fā)送一個預(yù)先格式化的時間,但沒有關(guān)于所述時區(qū)的任何信息.字符串如下:2013-08-26 16:55:00".
I have this backend that sends me a pre formatted time in a set time zone, but without any information for the said time zone. The strings are like: "2013-08-26 16:55:00".
我可以用這個字符串創(chuàng)建一個新的 moment.js 實例:
I can create a new moment.js instance with this string:
var time = moment("2013-08-26 16:55:00") //this creates time in my tz
但這只會在我自己的時區(qū)創(chuàng)建一個實例.
but this will only create an instance in my own time zone.
Moment.js 有一個插件,可以在特定時區(qū)創(chuàng)建對象的實例,效果很好,但我不能說我希望對象指向什么時間.
Moment.js have a plugin that can create instances of the object in specific time zones and it works great, but I can't say what time I want the object to point to.
如果我在紐約并且我這樣做:
If I'm in New York and I do this:
var time = moment("2013-08-26 16:55:00").tz("America/Los_Angeles");
結(jié)果時間將是 13:55 而不是 16:55,但在洛杉磯.
the resulting time will be 13:55 instead of 16:55 but in LA.
我想要創(chuàng)建一個顯示 16:55 但在洛杉磯時間的實例.
What I want is to create an instance that will say 16:55, but in LA time.
我問的原因是因為我想這樣做:
The reason I'm asking is because I want to do this:
var now = moment.tz("America/Los_Angeles");
var end = moment("2013-08-26 16:55:00"); //plus something to convert LA time
var timeLeft = end.diff(now, "minutes");
有沒有辦法做到這一點?
Is there a way to do that?
推薦答案
在大多數(shù)情況下,您可以簡單地這樣做:
In most cases, you can simply do this:
moment.tz("2013-08-26 16:55:00", "America/Los_Angeles")
如果需要輸入非ISO8601,則第二個參數(shù)指定格式字符串,第三個參數(shù)指定時區(qū):
If you require input other than ISO8601, then specify the format string as the second parameter, and the time zone as the third:
moment.tz("8/26/2013 4:55 pm", "M/D/YYYY h:mm a", "America/Los_Angeles")
而如果你需要使用moment的嚴(yán)格解析"模式,則進入第三個參數(shù),時區(qū)移動到第四個位置:
And if you need to use moment's "strict parsing" mode, then that goes in the third parameter, and the time zone moves to the fourth position:
moment.tz("8/26/2013 4:55 pm", "M/D/YYYY h:mm a", true, "America/Los_Angeles")
這篇關(guān)于如何使用 moment.js 在特定時區(qū)創(chuàng)建時間的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!