Remove timezone from a moment.js object

JavascriptDatetimepickerMomentjs

Javascript Problem Overview


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

Javascript Solutions


Solution 1 - Javascript

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

Solution 2 - Javascript

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.

Solution 3 - Javascript

Try this:

let str = '2015-10-01T15:40+01:00';
let moment = moment(str).utcOffset(str)
console.log(moment.format('DD/MM/YYYY HH:mm'))

<script src="https://momentjs.com/downloads/moment.js"></script>

Solution 4 - Javascript

This worked for me:

let date = "2017-02-01 15:20:00.00";
let pattern = "YYYY-MM-DD HH:mm:ss.SS"
let d = moment(date, pattern).utc(false);

It will still say that it considered the timezone, but I find that it doesn't.

LOG: 'ParseDateStr::2017-2-1 15:20:00.0000000
ParsedDate::Wed Feb 01 2017 15:20:00 GMT+0100 (Central European Standard Time)'

Normally, with just moment(date, pattern) it would parse as 16:20:00

Solution 5 - Javascript

Not a clean solution but you could do

momentDate.format('YYYY-MM-DDTHH:mm:ss') + '00:00';

This would remove the timezone and then append the '00:00' part back in. This is to extend Matt's answer.

Which would give you

> 2015-10-01T15:40:00+00:00

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlvaroView Question on Stackoverflow
Solution 1 - JavascriptMattView Answer on Stackoverflow
Solution 2 - JavascriptMatt Johnson-PintView Answer on Stackoverflow
Solution 3 - JavascriptFrofikeView Answer on Stackoverflow
Solution 4 - JavascriptWep0nView Answer on Stackoverflow
Solution 5 - JavascriptCraigView Answer on Stackoverflow