two different dates are returned in momentjs timezone for same set time?

Viewed 82

Even the value I set for the time in the moment.js timezones same following two different approaches the value of the date returned is different

  1. moment("02:00","HH:mm").tz('America/Chicago').format()

returns 2022-03-31T15:30:00-05:00

  1. moment().tz('America/Chicago').set({hour:"02",minute:"00"})

returns 2022-04-01T02:00:02-05:00

Can anyone please help me in understanding why is it so?

console.log(moment("02:00","HH:mm").tz('America/Chicago').format());
console.log(moment().tz('America/Chicago').set({hour:"02",minute:"00"}).format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.34/moment-timezone-with-data-10-year-range.js"></script>

1 Answers

You have different approach with two examples:

In first example you do:

  1. Take local date (2021-04-01 12:15)
  2. Set time for local date to 02:00 (2021-04-01 02:00)
  3. Move that particular local date to other time zone (so e.g. from +02:00 tz to -05:00 - moves back by 7 hours) (2021-03-31 15:00)

While in second example you do:

  1. Take local date (2021-04-01 12:15)
  2. Move that local date to other time zone (2021-04-01 06:15)
  3. Set time for moved time to 02:00 (2021-04-01 02:00)
Related