简体   繁体   中英

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. I do know the utc offset of both local and desired timezone. 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. If I can do this without using momentjs that is fine.

Take a look at the documentation for the subtract method:

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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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