Updating time offset with moment().utcOffset() Updating time offset with moment().utcOffset() express express

Updating time offset with moment().utcOffset()


The main issue is that you are passing the offset as a string instead of a number.

moment.utc("2015-10-01 01:24:21").utcOffset("-240").format('YYYYMMDD HHmmss ZZ')// "20151001 012421 +0000"moment.utc("2015-10-01 01:24:21").utcOffset(-240).format('YYYYMMDD HHmmss ZZ')// "20150930 212421 -0400"

When you have an offset in terms of minutes, then you must use the numeric form. You can always convert it:

moment.utc("2015-10-01 01:24:21").utcOffset(+"-240").format('YYYYMMDD HHmmss ZZ')// "20150930 212421 -0400"

Moment does allow for offsets to be passed as strings, but it expects them to be in one of the ISO8601 formats: either [+/-]HH:mm or [+/-]HHmm.

moment.utc("2015-10-01 01:24:21").utcOffset("-04:00").format('YYYYMMDD HHmmss ZZ')// "20150930 212421 -0400"

Additionally, note that I used moment.utc(...) to parse the input string. You just used moment(...) which will use the local time zone unless the time zone is explicit or if you pass a Date object instead of a string. It will also leave the moment object in "local mode", so your utcDate output would be wrong unless the time zone of the machine was actually set to UTC.

Lastly, don't forget "Time Zone != Offset". You can't assume that the offset you obtained is valid for all dates. If you need to project a date to to the user's time zone, you have to actually know the time zone, such as America/New_York. You can use these with the moment-timezone plugin.