简体   繁体   English

momentjs:如何在特定时区获取日期

[英]momentjs: how to get the date in a specific timezone

In a nutshell I want moment to respect server's timezone. 简而言之,我希望能够尊重服务器的时区。 I've set my machine's timezone to Alaska but I'm passing a Brisbane timezone string to moment. 我已经将机器的时区设置为阿拉斯加,但我正在通过布里斯班时区字符串。 Now I need moment.toDate to return a date instance in the same timezone as the one I pass in the moment constructor; 现在我需要moment.toDate来返回与我在当前构造函数中传递的日期实例相同的时区; eg 例如

m = moment("2016-11-20T08:00:00+10:00")
m.format() // "2016-11-20T08:00:00+10:00" 
m.toDate() // Sat Nov 19 2016 13:00:00 GMT-0900 (AKST) 

I want to get a Date instance from moment that's in the input timezone; 我希望从输入时区的瞬间获得一个Date实例; eg somehow get toDate to return Sun Nov 20 2016 08:00:00 GMT+1000 (AEST) . 如某种方式得到toDate返回Sun Nov 20 2016 08:00:00 GMT+1000 (AEST)

FWIW I have tried the same code with and without moment.tz.setDefault and while it correctly changes the format result, toDate always uses the machine's timezone! FWIW我尝试使用和不使用moment.tz.setDefault相同的代码,虽然它正确地更改了format结果, toDate总是使用机器的时区!

Update 更新

The reason I need this behaviour is that some JavaScript libraries and controls don't understand moment and only work with Date and the time/date gets skewed when presented back by them. 我需要这种行为的原因是一些JavaScript库和控件不理解moment ,只能使用Date ,时间/日期在被它们呈现时会出现偏差。 One example, the one I'm currently dealing with, is jQuery UI date picker control. 我正在处理的一个例子是jQuery UI日期选择器控件。 I want the date picker to show the current date as it's on the server (or on a specific timezone). 我希望日期选择器显示当前日期,因为它在服务器上(或在特定时区)。

Thanks in advance. 提前致谢。

The Date object represents the time in UTC internally, and can only use the time zone of the machine its running on when projected. Date对象在内部表示UTC时间,并且只能在投影时使用其运行的计算机的时区。

There's absolutely no way to produce a Date object that uses an arbitrary time zone. 绝对没有办法生成使用任意时区的Date对象。 Any examples you may come across that try to manipulate the Date object (such by adding or subtracting time zone offsets) are fundamentally flawed. 您可能遇到的任何尝试操作Date对象的示例(例如通过添加或减去时区偏移)都存在根本缺陷。

Moment itself has great time zone support, including the moment-timezone extension for working with named time zones instead of just time zone offsets. Moment本身具有很好的时区支持,包括使用命名时区而不仅仅是时区偏移的时刻 - 时区扩展。 But once you go back to a Date object - you're back at the mercy of the behavior of that object. 但是一旦你回到Date对象 - 你就会回到该对象的行为的摆布。

Sorry, but there's no way to achieve what you are asking. 对不起,但没有办法实现你的要求。 Perhaps you could elaborate as to why you wanted to do this, and I could recommend a workaround. 也许您可以详细说明为什么要这样做,我可以推荐一种解决方法。


Update: With regards to your update, usually there is a mechanism for getting the value from a date picker as text , rather than as a date object. 更新:关于您的更新,通常有一种机制可以将日期选择器中的值作为文本而不是日期对象。 With the example of the jQuery UI date picker control, the onSelect event gives it to you as text already, or you can simply call .val() instead of .datepicker('getDate') to get the text out of the field. 使用jQuery UI日期选择器控件的示例, onSelect事件已经将它作为文本提供给您,或者您可以简单地调用.val()而不是.datepicker('getDate')来从文本中获取文本。 Once you have a textual value, you can then parse it with moment however you like. 一旦你有了文本值,你就可以随心所欲地解析它。

Similarly, when setting the value, you don't necessarily need a Date object. 同样,在设置值时,您不一定需要Date对象。 You could just set the value of the textbox as a string, or you can pass a string to the setDate function. 您可以将文本框的值设置为字符串,也可以将字符串传递给setDate函数。

In most cases, you won't have to go through a Date object. 在大多数情况下,您不必通过Date对象。 But if for some reason you do, then you'll need to artificially construct one with something crazy like: 但如果由于某些原因你做了,那么你需要人为地构建一个像疯狂的东西:

var d = new Date(m.format('YYYY/MM/DD'));

Normally, I'd be against that - but if it's just there to get the pass a value to a UI control, then it's probably ok. 通常情况下,我会反对 - 但如果它只是为了获得传递给UI控件的值,那么它可能没问题。

This will get you a moment in the same timezone as the moment string, but toDate is always in the local timezone. 这将使您在与时间字符串相同的时区中获得片刻,但toDate始终位于本地时区。

d = "2016-11-20T08:00:00+10:00"
m = moment(d).utcOffset(d)
m.format()
m.toDate()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM