Several answers already mention that the time-zone is the path to the named time zone. I just want to clarify something in this library, which bothers me quite a bit. There is a difference between these two statements:
moment.tz(date, format, timezone) moment(date, format).tz(timezone)
Assuming the time zone is not specified in the date passed in:
The first code takes a date and assumes that the time zone is the one that was transmitted. The second accepts the date, receives the time zone from the browser, and then changes the time and time zone in accordance with the transmitted time zone.
Example:
moment.tz('2018-07-17 19:00:00', 'YYYY-MM-DD HH:mm:ss', 'UTC').format() // "2018-07-17T19:00:00Z" moment('2018-07-17 19:00:00', 'YYYY-MM-DD HH:mm:ss').tz('UTC').format() // "2018-07-18T00:00:00Z"
My time zone is +5 from UTC. Thus, in the first case, it does not change and sets the date and time for the utc time zone.
In the second case, he assumes that the transferred date is at -5, then turns it into UTC, and that is why he spits out the date "2018-07-18T00: 00: 00Z"
NOTE. The format parameter is really important. If a missed moment can return to the Date class, this can lead to unpredictable behavior.
Assuming the time zone is in the date passed in:
In this case, they both behave the same.
Despite the fact that I now understand why this is so, I thought it was a rather confusing feature that is worth explaining.
Nicola Pedretti Jul 13 '18 at 22:03 2018-07-13 22:03
source share