It sounds like you are trying to have the user pick a UTC-based date and time. Therefore, the best way would be to have the picker operate in UTC mode when it creates the moment to begin with. I'm not familiar with this particular datetimepicker, but assuming somewhere internally it does something like this:
var m = moment([year, month-1, day, hour, minute]);
Then is should instead do this:
var m = moment.utc([year, month-1, day, hour, minute]);
(The variables shown here would be coming from within the picker control.)
Ideally, the picker control should include a feature to set UTC mode so it can do this internally, when told to by you.
If it doesn't have such a feature, you can still compensate yourself. Unfortunately, you can't just call .utc()
, as that would give a different time than the one the user picked. So, you'll have to compensate by shifting the UTC time by the original moment's offset.
var m = // moment value from the picker
var result = moment(m).utc().add(m.utcOffset(), 'm');
You can then call format
or whatever you wish on the result. Notice that the original moment is cloned with moment(m)
, such that the offset doesn't get lost and the switch to UTC doesn't interfere with the picker's internal behavior.
Also, note that shifting like this is generally a hack, and if done wrong can lead to errors. Here it's ok, because the moment is already in UTC mode when the adjustment is applied. But as a general solution, shifting should be avoided. The best option is to have the control placed into UTC mode to begin with.