I ran into the same problem and found a few more ways in addition to the ones already written here.
Way №1
Let's say I type the following code in the console:
var TheDate = new Date(2012, 10, 5);
If you already have ready-made time numbers/string, for example [2012, 10, 5] or from <input type="datetime-local">, and you want to keep them as they are (as if they were UTC), then you can create string in Date time string format and use it:
let foo = new Date("2012-11-05T00:00:00Z")
console.log(foo.toUTCString()) // "Mon, 05 Nov 2012 00:00:00 GMT"
// or
let bar = new Date("2012-11-05T00:00:00+00:00")
console.log(bar.toUTCString()) // "Mon, 05 Nov 2012 00:00:00 GMT"
Attention: When the time zone offset is absent, date-only forms are interpreted as a UTC time and date-time forms are interpreted as a local time.
P.S.: If you only need a timestamp, you can get it using Date.parse():
let ts = Date.parse("2012-11-05T00:00:00Z")
// or
//let ts = Date.parse("2012-11-05T00:00:00+00:00")
console.log(ts) // 1352073600000
console.log(new Date(ts).toUTCString()) // "Mon, 05 Nov 2012 00:00:00 GMT"
Way №2
After creating a Date object, you can change its inner time using an offset to the environment timezone, this will save the time like it's in UTC.
let d = new Date(2012, 10, 5);
console.log(d.toUTCString()); // "Sun, 04 Nov 2012 23:00:00 GMT"
d.setTime( d.getTime() - d.getTimezoneOffset() * 60 * 1000 );
// 1352059200000 - 60(minutes) * 60(seconds) * 1000(milliseconds)
console.log(d.toUTCString()); // "Mon, 05 Nov 2012 00:00:00 GMT"
Source: Matt Johnson-Pint answer to the question "get UTC date (not UTC string) in javascript"