-1

I thought I had a handle on this, but I cant work it out.

scenario:
1) user selects a date widget which passes back a date in local timezone, lets say 10am 'Australia/Sydney'

2) user then selects a timezone that is different, by identifier 'Australia/Brisbane' (this is a different TZ and may have daylight saving etc...) lets assume its +1hr

What I want to do is have a function that takes a Date object that represents [10am 'Australia/Sydney'] and return to me a new Date that represents [10am 'Australia/Brisbane] i.e. the underlying UTC time will have shifted +1hr

 function convertToTimezone(date, newTimezone) {

      ... what goes here? ...

      return newDate;
 }

Ive been mucking about with moment timezone and I cant get it to do what I want.

Steve K
  • 4,863
  • 2
  • 32
  • 41
  • `I cant get it to do what I want` to be perfectly blunt, it's not clear what you want to do – Bravo Oct 31 '18 at 00:11
  • But during daylight saving, 10 am in Sydney is 9 am in Brisbane (and 23:00 the previous day UTC). That's why you should always work in UTC behind the scenes and only present local times in the UI. So if someone in Brisbane books a meeting for say 13:00 local, it's 03:00 UTC. Now anyone can open the meeting and see their local time, so for someone in Sydney it's 14:00 (UTC+11), in Perth 11:00 (UTC+8) and in Mubai 08:30 (UTC +5:30) and so on… – RobG Oct 31 '18 at 01:58

2 Answers2

0

The moment-timezone library should make this trivial:

function convertToTimezone(date, newTimezone) {
    return moment(date).tz(newTimezone);
}

Or if date is already a moment:

function convertToTimezone(date, newTimezone) {
    return date.clone().tz(newTimezone);
}

See the documentation on Converting to Zone for more information.

Alex Taylor
  • 8,343
  • 4
  • 25
  • 40
  • How do you compare two cityes? Say Zagreb/Mumbay or Sidney/Brisbane, as-per - setting the date-time of A and get the accurate date-time of B (which say at the time of comparing is already into DTS)? – Roko C. Buljan Oct 31 '18 at 00:32
  • I hope I'm understanding you correctly. The date/moment will have timezone information embedded in it. Using moment makes comparisons easy (https://momentjs.com/docs/#/query/), or getting the duration between two times (https://momentjs.com/docs/#/durations/). – Alex Taylor Oct 31 '18 at 00:45
  • see my answer. I havent tested your code specifically, but I had code very much like it that failed to return me a date with the same wallclock time in a different timezone. – user3769865 Nov 01 '18 at 00:54
0

OK, FWIW, I got an answer myself. moment.tz doesnt work as I imagined.

To summarise, I want to take a javascript Date that has a wallclock time, say '10am on the 15 sep 2018' that has been associated with a certain timezone identifier, say BrisabneOz.

And turn it into a new date that represents that same wallclock time, but in a different timezone. In otherwords, change the underlying UTC time by the amount required by the shift in timezones and/or daylight savings etc...

The way I found to do this was to get the string of the wallclock date, thus stripping any associated timezone from tbe equation, and using moment.tz to make another date object using the new different timezone. Which it can do.

The part that confused me was having to go to a string as a step - thought I could just pass in one date and get moment.tz to magic me up another date ala @Alex Taylor answer, but this doesnt actually work.

function convertDateToTimezone(date, timezone) {
    const str = moment(date).format('YYYY-MM-DD HH:mm:ss');
    const tzMoment = moment.tz(str, timezone.identifier)
    return tzMoment.toDate()
}