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.