moment.js:如何获取特定时区的日期 [英] momentjs: how to get the date in a specific timezone

查看:1842
本文介绍了moment.js:如何获取特定时区的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之,我想时刻尊重服务器的时区.我已将机器的时区设置为阿拉斯加,但现在正在传递布里斯班时区字符串.现在,我需要moment.toDate来返回一个与实例中构造函数传递的时区相同的时区的日期实例.例如

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; e.g.

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) 

我想从输入时区中获取一个Date实例;例如以某种方式使toDate返回Sun Nov 20 2016 08:00:00 GMT+1000 (AEST).

I want to get a Date instance from moment that's in the input timezone; e.g. somehow get toDate to return Sun Nov 20 2016 08:00:00 GMT+1000 (AEST).

首先,我尝试了使用和不使用moment.tz.setDefault的相同代码,尽管它可以正确更改format的结果,但是toDate始终使用计算机的时区!

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!

更新

我需要此行为的原因是,某些JavaScript库和控件不理解moment,只能与Date一起使用,并且当它们返回时,时间/日期会出现偏差.我当前正在处理的一个示例是jQuery UI日期选择器控件.我希望日期选择器显示服务器(或特定时区)上的当前日期.

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. One example, the one I'm currently dealing with, is jQuery UI date picker control. I want the date picker to show the current date as it's on the server (or on a specific timezone).

谢谢.

推荐答案

Date对象在内部表示UTC的时间,并且只能使用其运行时所在计算机的时区.投射.

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对象.您可能遇到的任何尝试操纵Date对象的示例(例如通过添加或减去时区偏移量)都存在根本缺陷.

There's absolutely no way to produce a Date object that uses an arbitrary time zone. Any examples you may come across that try to manipulate the Date object (such by adding or subtracting time zone offsets) are fundamentally flawed.

Moment本身具有强大的时区支持,包括用于使用命名时区(而不只是时区偏移量)的moment-timezone扩展.但是,一旦返回到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. But once you go back to a Date object - you're back at the mercy of the behavior of that object.

对不起,但是无法实现您的要求.也许您可以详细说明为什么要这样做,我可以建议一种解决方法.

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.

更新:关于您的更新,通常存在一种机制,用于从日期选择器中获取作为 text 而不是作为日期对象的值.在jQuery UI日期选择器控件的示例中,onSelect事件已将其作为文本提供给您,或者您可以简单地调用.val()而不是.datepicker('getDate')来使文本脱离字段.一旦有了文本值,就可以随心所欲地对其进行解析.

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. Once you have a textual value, you can then parse it with moment however you like.

类似地,在设置值时,您不一定需要Date对象.您可以将文本框的值设置为字符串,也可以将字符串传递给 setDate 函数.

Similarly, when setting the value, you don't necessarily need a Date object. You could just set the value of the textbox as a string, or you can pass a string to the setDate function.

在大多数情况下,您不必遍历Date对象.但是,如果您出于某种原因这样做,那么您将需要人为地构建一个疯狂的东西,例如:

In most cases, you won't have to go through a Date object. 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'));

通常,我反对这样做-但是,如果只是为了将值传递给UI控件而已,那可能就可以了.

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.

这篇关于moment.js:如何获取特定时区的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆