Unzone the toDate of a moment

Viewed 17

My server is in UTC time.

I create a localized date time in Los Angeles with moment like this:

var m = moment.tz('2021-04-13T20:30:53-07:00', 'America/Los_Angeles');

This part works great. Doing m.date() gives me 13. However I need to give it to a lib as a Date object, not a Moment so what I do is:

var d = m.toDate()

However now my server gets the wrong day. As it's in UTC it does d.getDate() which gives it the 14th, as UTC is 7 hours ahead so this timestamp is 3:30am.

Is there a way in moment to get the unzoned date? Such that even in UTC d.getDate() will give 13th.

I wrote this custom function, but I was hoping the moment-timezone lib provided this.

const getUnzonedDate = (m) => {
    return m.clone().add(m.utcOffset(), 'minutes').toDate();
}
Another example

The above getUnzoned date works if my system time is in UTC. Let's say my system time is in LA:

   * Removes zone information means, if system time is LA:
   * 
   * const m = moment.tz('2021-04-13T21:30:53', 'America/New_York');
   * m.hours() === m.toDate().getHours(); // false
   * m.hours() === getUnoznedDate(m).getHours(); // true
   * 
   * So it means that when the date will give you same numbers for each unit in
   * moment and as a date.
0 Answers
Related