javascript - Is getTimezoneOffset() stable during daylight saving transition? -


i have date time below conversion logic in javascript converting utc time passed timezone's local time. wondering logic work fine during daylight transition? if not remedy that? can't use third party library. have 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>

gettimezoneoffset gets offset host system, "stable" host.

note: parsing of strings date constructor entirely implementation dependent , unreliable, don't it. manually parse strings. library can help, parsing function particular format 2 lines (3 validation).

when change "2016-04-10 09:00:00.0" "2016-04-10t09:00:00.0" have created iso 8601 date , time string without timezone. if parsed correctly (about 10% of browsers in use won't parse @ all), should treated local date , time (chrome treats incorrectly utc, ie 11 correctly local), host system settings used adjust created date's utc time value represents same moment in time.

e.g.

alert(new date("2016-04-10t09:00:00.0")); 

should print date 10 april, 2016 09:00 regardless of host system's timezone offset (but not in browsers treat utc, wrong).

if want find out equivalent time in other time zone, use utc methods adjust date (or set time value directly you've done) desired offset. use utc methods read date , time values.

e.g. find equivalent time in utc+05:30 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) + ' @ utc+05:30');


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -