Search code examples
ruby-on-railsruby-on-rails-5momentjsmoment-timezone

Storing and comparing UTC times


Say this is the current server time in UTC 2021-07-30 00:33:25 UTC, which in local time is just 2021-07-29 5:33 PM 25 hours from that time is 2021-07-31 01:33:25 UTC, which in local time is 2021-07-30 6:33 PM The stored time being compared is saved as 2021-07-30T19:00:00+00:00, which in local time should be 2021-07-30 7:00 PM

The problem is that the stored time should be 2021-07-31T02:00:00+00:00 to equal 2021-07-30 7:00 PM, but I'm not sure of the best approach to get that time.

Here's how I get the time being sent to my rails backend

  import * as moment from 'moment-timezone';
  ...
  const start_time = moment
    .tz(
      listingAvailability.listing_availability_ranges_attributes[0]
        .start_time,
      'YYYY-MM-DD hh:mm a'
    )
    .tz('America/Phoenix')
    .toISOString(true)



Solution

  • I fixed it with

    moment.utc(moment(listingAvailabilityTime).utc().local()).format();