简体   繁体   English

我如何使用javascript更改日期的时区而不更改时间

[英]how do i use javascript change timezone of date without changing the time

I have users who's local timezone can be different from a facility they are setting up event start times for. 我的用户所在的本地时区可能不同于为其设置事件开始时间的设施。 I want to take the time (say 10 am) and change the javascript date object to be 10am in the facility timezone and not the local timezones. 我想花点时间(例如上午10点),并将javascript日期对象更改为设施时区而非本地时区的上午10点。 I do know the utc offset of both local and desired timezone. 我确实知道本地和所需时区的utc偏移量。 I have not been able to link it all together. 我无法将所有内容链接在一起。 Here is my example. 这是我的例子。

var localOffset = 420;
var desiredOffset = 360;
var eventStartDate = moment(localStartDate);
eventStartDate.subtract(localOffset - desiredOffset);

my original time is in MDT and my desired is CDT but the start time of 10am needs to remain the same. 我原来的时间是MDT,而我希望的是CDT,但是上午10点的开始时间必须保持不变。 If I can do this without using momentjs that is fine. 如果我可以不使用momentjs就可以做到这一点。

Take a look at the documentation for the subtract method: 请看一下subtract方法的文档:

http://momentjs.com/docs/#/manipulating/subtract/ http://momentjs.com/docs/#/manipulating/subtract/

You're missing a unit in your call. 您在通话中错过了一个部门。 It appears that you're using minutes, so your call should be something like: 看来您正在使用分钟,所以您的通话应类似于:

eventStartDate.subtract(localOffset - desiredOffset, 'minutes');

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

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