0

How can I convert a date to UTC from a specific timezone?

In javascript you can convert a local date to utc and create a date from a date string or utc string. The intl built in functions allow you to convert a datetime to a timezone, but not back to utc. I could not find any specific questions on this amazingly all others say local time.

I know that in moment you can convert a UTC from a timezone, like this:

var now = moment();
console.log(now.format('YYYY-MM-DD HH:mm:ss'))
console.log(now.utc().format('YYYY-MM-DD HH:mm:ss'))

console.log(now.tz("Australia/Sydney").format('YYYY-MM-DD HH:mm:ss'))
console.log(now.tz("Australia/Sydney").utc().format('YYYY-MM-DD HH:mm:ss'))
console.log(now.tz("Australia/Sydney").tz("Asia/Tokyo").format('YYYY-MM-DD HH:mm:ss'))
console.log(now.tz("Australia/Sydney").tz("Asia/Tokyo").utc().format('YYYY-MM-DD HH:mm:ss'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.25/moment-timezone-with-data.min.js"></script>

The utcs should match. Aside from using moment, what other ways (browser, node, libraries) are there to convert a date considering it's in a timezone that is not local in the browser using js to utc? Doesn’t need to be vanilla js.

lastlink
  • 1,505
  • 2
  • 19
  • 29
  • What is it you are actually trying to achieve here? The question is not clear. Are you just wanting to find a vanilla JS way to convert from a specific timezone to UTC? – Herohtar May 17 '19 at 23:32
  • I think you're looking for [this answer](https://stackoverflow.com/a/53652131/574531). – Herohtar May 17 '19 at 23:51
  • While that answer is helpful I specifically wanted to take a date that is not in the users local timezone and convert to utc so i could then do other stuff with it like convert to local to timezone or another timezone. Doesn’t need to be vanilla. There are functions like intl that let you convert a date to a different timezone but not back. Lots of use cases for going backwards. – lastlink May 18 '19 at 01:41
  • If you look at the linked answer you'll see that it actually does exactly that -- you specify a time and a timezone and it will return the UTC time as if the time you specified was in the timezone you gave it rather than local. – Herohtar May 18 '19 at 02:13
  • 1
    @Herohtar - unfortunately, that answer is flawed as well. I will give more details as to why when I have a bit more time. ;) – Matt Johnson-Pint May 18 '19 at 04:51

1 Answers1

0

Here was my javascript native vanillajs solution using Intl.DateTimeFormat()

Basically I get the difference in the timezone specified and the local time. I add that difference then set return the utc.

/**
 * take a date of assumed timezone and convert to utc
 *
 * @param {*} d
 * @param {*} tz
 * @returns
 */
function tzUTC(d, tz) {
  // first calculate tz difference
  // use passed in date to get timezone difference as close to that day.
  var date = new Date(d);
  var options = {
    year: 'numeric',
    month: 'numeric',
    day: 'numeric',
    hour: 'numeric',
    minute: 'numeric',
    second: 'numeric',
    hour12: false,
    timeZone: tz
  };

  var tzDate = new Intl.DateTimeFormat('en-US', options).format(date)
  var diff = date - new Date(tzDate);
  var minutes = Math.floor((diff / 1000) / 60);
  var localTime = new Date(d);
  localTime.setMinutes(d.getMinutes() + minutes);
  return localTime.toUTCString();
}


var d = new Date("5/18/2019, 07:49:13");
// Fri May 17 2019 17:49:13 GMT-0400 (Eastern Daylight Time)
// utc should be Fri, 17 May 2019 21:49:13 GMT"
// 
console.log("d:" + d)
console.log("tzUTC:" + tzUTC(d, 'Australia/Sydney'))

d = new Date("5/17/2019, 14:53:21");
console.log("d:" + d)

// Fri May 17 2019 17:53:21 GMT-0400 (Eastern Daylight Time)
// utc "Fri, 17 May 2019 21:53:21 GMT"

console.log("tzUTC:" + tzUTC(d, 'America/Los_Angeles'))
lastlink
  • 1,505
  • 2
  • 19
  • 29
  • 1
    Some careful unit testing near transitions may help you understand why this approach isn't sound, but I'll point out two issues: 1) The difference between the offsets of two given time zones is not necessarily the same for all dates and times. 2) Not every datetime from one time zone exists in every other time zone. – Matt Johnson-Pint May 18 '19 at 04:44