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.
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>