-
Notifications
You must be signed in to change notification settings - Fork 838
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Explicit time zones break utcOffset(..., true) #187
Comments
utcOffset('...', true)
The Also, by wrapping the moment in another moment, you're cloning it, so the second line of each block isn't manipulating anything other than the moment used by the That said, I actually don't get the same results as you showed. In the second example, I get @timrwood any ideas? |
Yeah I get Just to clarify: I'm not using |
The value passed to |
I still get inconsistent results with var m1 = moment('2014-11-21T10:30:00-06:00');
m1.utcOffset(0, true).format();
// "2014-11-21T10:30:00+00:00"
var m2 = moment.tz('2014-11-21T10:30:00', 'America/Chicago');
m2.utcOffset(0, true).format();
// "2014-11-21T04:30:00-06:00" |
Ok, I think I see what is going on here. While What you should do is remove the timezone from the moment and then set the offset. Right now there is no official way to remove a timezone, but you could do It is kinda weird to parse into a timezone, then switch to another offset while keeping the same time. Why not just do |
I'm receiving a datetime from an API in UTC and converting it to a user's timezone for display. That seems like a pretty valid use-case? |
If you want to display a utc datetime in local time you can do this. moment.utc('2014-11-21T10:30:00').local().format() // 2014-11-21T04:30:00-06:00 If you want to display a utc datetime in a specific timezone you can do this. moment.utc('2014-11-21T10:30:00').tz('America/Los_Angeles').format() // 2014-11-21T02:30:00-08:00 Maybe I'm missing the use case, but do those help? |
The problem I have is that I can't guarantee the original timestamp is UTC, which is why I'm initializing with |
I think I have the same problem here. I am creating a moment object from a utc timestamp, and setting the timezone of the object. However, when returning
Gives: So the timezone is set correctly, but |
@ashleydw - |
sorry my fault! I was reading it incorrectly |
|
@wyefei - in the example you gave, the offset is your local offset, not the one passed in. You'd have to use |
To be clear, the bug being tracked in this issue is related to calling This should be relatively easy to fix, so marking up for grabs. |
For reference, here is a similar example, although with the false parameter.
which results in this output:
The expectation is that the output object 2 and object 5 would be the same. The workaround appears to be converting the input moment object to the UTC timezone (to remove the association with a DST timezone) and then adjust the utcOffset (see m6 above) |
Is this still a bug, if not would love to work on it. |
I'm trying to use the
utcOffset
with the keepLocalTime parameter as true. It looks like if you use an explicit time zone when creating the object, it sets the the internal property_isUTC
to true, which breaks theutcOffset
.The text was updated successfully, but these errors were encountered: