0

Check the following code sample:

moment.utc("2014-10-19T09:27:57.9417128+00:00")
      .diff(moment.utc("2014-10-19T09:27:57.9417128+02:00"))

I would expect 0 since I'm converting both dates to UTC, but this gives 7200000 as result.

In fact, I'm looking to get moment.fromNow or moment.from to work with UTC in order to get a X seconds/minutes/hours... ago without an invalid result because of Date/moment translating date-times based on the date's offset.

What am I doing wrong here?

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206

1 Answers1

1

I'm not sure why you would think the source offsets should be ignored. They are especially relevant for converting to UTC, because they actually represent the difference between UTC and the time represented.

In the first timestamp, the +00:00 means the time is already at UTC. In the second timestamp, the +02:00 means the time is two hours ahead of UTC. 2 * 60 * 60 * 1000 = 7200000.

In other words:

    2014-10-19T09:27:57.9417128+00:00  ==  2014-10-19T09:27:57.9417128Z
-   2014-10-19T09:27:57.9417128+02:00  ==  2014-10-19T07:27:57.9417128Z
=======================================================================
                                                      02:00:00

There is no way the result should be zero, because any way you look at it, the two timestamps represent two different moments in time that are separated by two hours.

Since moment's fromNow function already works with the current UTC time, and you have a full ISO timestamp with an offset, you can just use it directly without any conversion.

moment("2014-10-19T09:27:57.9417128+02:00").fromNow()

You don't even need to convert to UTC first. You could do it like this:

moment.utc("2014-10-19T09:27:57.9417128+02:00").fromNow()

But these will both return the same thing because you have already supplied the offset. They would only differ if you didn't include an offset, in which case the first example would interpret the input string in local time and the second case would interpret the input string in UTC. Neither of which change the behavior of the fromNow function.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • I've removed my answer for now, but I believe we're confusing things. What I'm trying to achieve is getting how many time has passed since something happened regardless of the offset. I mean: if something happened now and someone wants to know how many time has passed since the first happened, it'll be always the same for anyone in the world, even if the user is in +08:00 offset and the server is +02:00 – Matías Fidemraizer Oct 19 '14 at 19:12
  • Sorry, but that's not true. I'm not sure why you would think that. The offset is a reflection of the timestamp. For example, on my clock right now, it's 12:17PM and my offset is -07:00. For someone in +02:00, it is right now 9:17PM. For both of us, it's 7:17PM UTC. – Matt Johnson-Pint Oct 19 '14 at 19:19
  • For me, when I do the `from` call, with my and your date, I would get that "something happened 7 hours ago" and this isn't true – Matías Fidemraizer Oct 19 '14 at 19:45
  • Without the offset, you might get that. But the offset ensures that wont happen as long as it was recorded correctly to begin with. If for some strange reason you are disassociating the offset with the timestamp when you recorded it, that might explain the behavior you described, but under normal circumstances, that shouldn't happen. – Matt Johnson-Pint Oct 19 '14 at 20:00
  • So, do you still think my answer was wrong? If I want to know how much time has passed since some event happened in the world, I need both date-times in UTC (+00:00). – Matías Fidemraizer Oct 19 '14 at 20:05
  • Your other answer was wrong because it used the local computer's offset, not the timestamp's offset. You are correct that to compare, both values need to be in UTC. `fromNow` uses the current UTC time (regardless of the local computer's time zone), and the time stamp sting you provided has an offset. So that offset is used to adjust the time stamp back to UTC, then the two values are compared. Make sense? – Matt Johnson-Pint Oct 19 '14 at 20:33
  • Earlier when you said "something happened 7 hours ago" wasn't true *for you*, you were likely not considering the offset in the "something", but rather your own offset. If you were to adjust properly to your own time zone, you would find that it was indeed 7 hours ago. – Matt Johnson-Pint Oct 19 '14 at 20:47
  • I'm not sure if we're understanding the issue (maybe I'm missing something). Imagine it's a chat and someone posts a message and the other receives the whole message. The message is in the chat history and both users are in different time zones. This won't mean there're many hours of difference, because the event happened some seconds ago. – Matías Fidemraizer Oct 19 '14 at 20:49
  • The offset needs to be in the time stamp in the chat history. Then it all works out. It wont show they are hours apart unless you ignore those offsets. – Matt Johnson-Pint Oct 19 '14 at 20:57
  • As I pointed out in my question, it's not working as expected. Try it yourself – Matías Fidemraizer Oct 19 '14 at 21:04
  • Here. Check out [this jsFiddle](http://jsfiddle.net/z01vuk9x/) showing a chat log between two parties in different time zones. Each message is one minute apart. – Matt Johnson-Pint Oct 19 '14 at 22:42
  • You're right. Do you know what was the issue? I was still using moment 2.6.0. It seems like 2.6 had the issue, because I switched to 2.8.0 and it works like you showed me in the jsFiddle. In fact, my first tries was just doing like you, but since I wasn't getting the expected result............ – Matías Fidemraizer Oct 20 '14 at 04:28
  • Glad to help! I'm not sure which bug it was that was fixed, but if you figure it out, let me know. Cheers! – Matt Johnson-Pint Oct 20 '14 at 04:36
  • I'm nearly to be sure that my first attemp was doing just like your fiddle, and I was getting unexpected results. Then, I thought: what if I turn dates into UTC+00:00? And it worked. Maybe I'm mistaken, but this has been the sequence :D – Matías Fidemraizer Oct 20 '14 at 08:31
  • Hey... I've published another question http://stackoverflow.com/questions/26483750/moment-fromnow-moment-from-returning-unexpected-ago-text-when-working-with-utc which is related to this... Either I'm understanding this at all or there's some issue here grr – Matías Fidemraizer Oct 21 '14 at 09:55