First thing off, I know how to convert the a local date to UTC in Javascript. This isn't a duplicate of that.
I've been trying to get a function that will convert a date to UTC if it isn't already, I don't have control whether or not the input is UTC or not. I can't use Javascript plugins.
Normal solutions would be just to parse a local date into UTC, but parsing a date that's already UTC that way outputs the weird behavior of modifying the already UTC date incorrectly.
I have an example function below.
function temp() {
var utc = new Date(Date.UTC(2017, 1, 10, 10, 10, 0));
var local = new Date(2017, 1, 10, 10, 10, 0);
var utc2 = new Date(utc.getUTCFullYear(), utc.getUTCMonth(), utc.getUTCDate(), utc.getUTCHours(), utc.getUTCMinutes(), utc.getUTCSeconds());
var local2 = new Date(local.getUTCFullYear(), local.getUTCMonth(), local.getUTCDate(), local.getUTCHours(), local.getUTCMinutes(), local.getUTCSeconds());
var utc3 = new Date(utc.getTime() + utc.getTimezoneOffset()*60*1000);
var local3 = new Date(local.getTime() + local.getTimezoneOffset()*60*1000);
Logger.log("Timezone offset: UTC=" + utc.getTimezoneOffset() + " Local=" + local.getTimezoneOffset());
Logger.log("UTC: " + utc.getTime() + " -> UTC2(" + utc2.getTime() + ") or UTC3(" + utc3.getTime() +")");
Logger.log("Local: " + local.getTime() + " -> UTC2(" + local2.getTime() + ") or UTC3(" + local3.getTime() +")");
}
returns
Timezone offset: UTC=420 Local=420
UTC: 1486721400000 -> UTC2(1486746600000) or UTC3(1486746600000)
Local: 1486746600000 -> UTC2(1486771800000) or UTC3(1486771800000)
Note that even the UTC date has a timezone offset. And that both methods of converting a local date to UTC strangly modifies the already UTC date to something incorrect.
Thank you for your help.