Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
155
rated 0 times [  159] [ 4]  / answers: 1 / hits: 94502  / 9 Years ago, thu, october 1, 2015, 12:00:00

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


More From » express

 Answers
15

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.


[#64885] Monday, September 28, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
beatrices

Total Points: 745
Total Questions: 103
Total Answers: 105

Location: Guam
Member since Tue, Nov 29, 2022
2 Years ago
beatrices questions
Fri, Jan 14, 22, 00:00, 2 Years ago
Sun, Feb 14, 21, 00:00, 3 Years ago
Fri, Nov 20, 20, 00:00, 4 Years ago
Mon, Aug 12, 19, 00:00, 5 Years ago
;