1

This post was very useful Getting the date of next Monday when it comes to figuring out how to get the next Monday in UTC time. I have the following problem.

I want to get the next Monday in user A's timezone and then convert that to user's B timezone. How can I do that?

So if user A's timezone is America/New_York and the date is January 2, 2022, I want to get the next Monday in that timezone. Then convert that date to user B's timezone. How can I do that using javascript or moment js?

The goal is to get the next availability of a user so that another user in a different timezone can set up an appointment with him.

Rue Vitale
  • 1,549
  • 4
  • 17
  • 24
  • Not sure I follow... how exactly are you sending the date from client A to B? – skara9 Jan 15 '22 at 00:11
  • you know the date for next Monday, and you know the two timezones involved, so create a new Date object _with that timezone_ as part of the constructor string, and then take it from there? – Mike 'Pomax' Kamermans Jan 15 '22 at 00:13

1 Answers1

0

If I'm getting your question right you want to get the date at a different time zone then get the next Monday then convert it to a time zone you want. If so this should work:

Note: this uses Moment js and moment time zone libraries. The "moment time zone with data" library is a little bulky so if you are concerned about performance then CDN might not be the way to go try node js.

function getNextMonday() {
  const now = new Date()
  const today = new Date(now)
  today.setMilliseconds(0)
  today.setSeconds(0)
  today.setMinutes(0)
  today.setHours(0)

  const nextMonday = new Date(today)

  do {
    nextMonday.setDate(nextMonday.getDate() + 1) // Adding 1 day
  } while (nextMonday.getDay() !== 1)

  return nextMonday
}

var cur_date = moment(getNextMonday());
//this gives the corrsponding date on specific time zone of next monday
console.log(cur_date.tz('America/Los_Angeles').format())
<script src="https://momentjs.com/downloads/moment.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.31/moment-timezone-with-data.js"></script>
seriously
  • 1,202
  • 5
  • 23
  • The issue here is that cur_Date is not in user A's timezone. It's in UTC. If User A is in America/New_York and User B is in China, it should get the next monday for user A's timezone and not UTC. – Rue Vitale Jan 15 '22 at 12:47