Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating time offset with moment().utcOffset()

I'm facing some issue trying to use moment.js for dealing with time offsets.

I collect in an hidden input the local user time offset:

<script type="text/javascript">   $(document).ready(function () {      $('input#timeoffset').val(moment().utcOffset());    }); </script> 

The offset gets correctly stored (in my case its value is -240). Later on the server side (which runs in utc time) I try to update some db stored utcDate doing something like:

var userDate = moment(utcDate).utcOffset(offset) 

My issue is the following: if I run my code as above described I get no effects:

  • utcDate: 20151001 012421 +0000
  • userDate: 20151001 012421 +0000

If I flip the offset sign I get:

  • utcDate: 20151001 012421 +0000
  • userDate: 20151001 052421 +0400

I'm clearly doing something wrong (even if my expectation was that the first version was correct), do you have any hint?

On client-side I'm using moment.js v2.10.6 while on the server-side moment-timezone.js v0.4.0 and moment.js v2.10.6

like image 842
Pierluigi Avatar asked Oct 01 '15 01:10

Pierluigi


People also ask

How do I add offset time to my moment?

utcOffset(); // (-240, -120, -60, 0, 60, 120, 240, etc.) Setting the UTC offset by supplying minutes. The offset is set on the moment object that utcOffset() is called on. If you are wanting to set the offset globally, try using moment-timezone.

What is UTC offset in moment?

We can use pass value to utcOffset method as follows − var day = moment(). utcOffset(120); In the above, we are adding 120 minutes to the current moment and its output is displayed as below showing the current date/time and after adding offset.

How do I apply for UTC offset?

A UTC offset is the difference in hours and minutes between a particular time zone and UTC, the time at zero degrees longitude. For example, New York is UTC-05:00, which means it is five hours behind London, which is UTC±00:00.

How do I change timezone in Momentjs?

To change the default time zone, use moment. tz. setDefault with a valid time zone.


1 Answers

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.

like image 99
Matt Johnson-Pint Avatar answered Sep 19 '22 19:09

Matt Johnson-Pint