问题
I have date time below conversion logic in Javascript which is converting the UTC time into passed timezone's local time. I am wondering this logic will work fine during Daylight transition? If not what is the remedy for that? I can't use any third party library. I have to use pure javascript or Angular JS.
function myTime()
{
var d1= document.getElementById("txtDate").value;
var zOffset = document.getElementById("txtOffset").value;
console.log("Date1",d1);
var d2 = new Date(d1.replace(/ /g,'T'));
var d3= d2.getTime()+(d2.getTimezoneOffset()*60000);
console.log("Date2",d2);
console.log("Date3",d3);
var d4 = new Date(d3 + (3600000 * zOffset));
console.log("Date3",d3);
console.log("Date 4",d4);
var d5 = d4.toLocaleTimeString();
console.log("Date 5",d5);
//d6 =d5.match(/(\d+)(?=:\d+:\d+)|([A-Z]+)(?=$)/g).join(" ");
//console.log("Date6",d6)
document.getElementById("hourValue").innerHTML = d5;
}
<h1>
TimeZone
</h1>
<h2 id="hourValue">
</h2>
<table>
<tr>
<td>Date Time</td>
<td><input type="text" id="txtDate" value="2016-04-10 09:00:00.0" /></td>
</tr>
<tr><td>TimeZone</td></tr>
<tr><td><input type="text" id="txtOffset" value="-5.00" /></td></tr>
<tr><td><input type="submit" id="btnSubmit" value="Convert" onClick="myTime()" /></td></tr>
</table>
回答1:
getTimezoneOffset simply gets the offset from the host system, so it is as "stable" as the host.
Note: parsing of strings with the Date constructor is almost entirely implementation dependent and very unreliable, just don't do it. Always manually parse strings. A library can help, but a parsing function for a particular format is 2 lines (3 with validation).
When you change "2016-04-10 09:00:00.0" to "2016-04-10T09:00:00.0" you have created an ISO 8601 date and time string without a timezone. If parsed correctly (about 10% of browsers in use won't parse it at all), it should be treated as a local date and time (Chrome treats it incorrectly as UTC, IE 11 correctly as local), so the host system settings will be used to adjust the created date's UTC time value so that it represents the same moment in time.
e.g.
alert(new Date("2016-04-10T09:00:00.0"));
should print a date for 10 April, 2016 09:00 am regardless of the host system's timezone offset (but not in browsers that treat it as UTC, which is wrong).
If you want to find out the equivalent time in some other time zone, use UTC methods to adjust the Date (or set the time value directly as you've done) for the desired offset. Then use UTC methods to read the date and time values.
e.g. to find the equivalent time in UTC+05:30 you might do:
function toLocalISO(d){
function z(n){return (n<10?'0':'') + n}
return d.getUTCFullYear() + '-' + z(d.getUTCMonth()+1) + '-' + z(d.getUTCDate()) + 'T' +
z(d.getUTCHours()) + ':' + z(d.getUTCMinutes()) + ':' + z(d.getUTCSeconds())
}
var d = new Date('2016-04-10T09:00:00.0');
document.write('When it\'s : ' + d + ' here<br>');
d.setUTCMinutes(d.getUTCMinutes() + 330);
document.write('It\'s : ' + toLocalISO(d) + ' at UTC+05:30');
来源:https://stackoverflow.com/questions/35834769/is-gettimezoneoffset-stable-during-daylight-saving-transition