I'm using datetimepicker.js and its date
function returns a moment.js object. It does so with the local UTC offset in it and my original date has a different offset.
My original date:
2015-10-01T15:00:00.000Z
What I display on the date time picker (DD-MM HH:mm
):
01-10 15:00
What I get:
2015-10-01T15:40:00+01:00
What I want:
2015-10-01T15:40:00+00:00
Note how I removed the +01 offset at the end.
How can I do this applying it for any local UTC ? This is, without having to manually remove the 01 (as it can be a any other local offset depending on the user location).
var momentDate = timePicker.data("DateTimePicker").date(); console.log(momentDate.format()); //this prints 2015-10-01T15:40:00+01:00
momentDate. format('YYYY-MM-DDTHH:mm:ss') + '00:00'; This would remove the timezone and then append the '00:00' part back in.
To change the default time zone, use moment. tz. setDefault with a valid time zone.
The main moment. js library has full functionality for working with UTC and the local time zone.
To get the current date and time, just call moment() with no parameters. var now = moment(); This is essentially the same as calling moment(new Date()) . Note: From version 2.14.0, moment([]) and moment({}) also return now.
You need to explicitly specify the format.
Try this:
momentDate.format('YYYY-MM-DDTHH:mm:ss')
this will give the result as
2015-10-01T15:40:00
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With