asp.net - getTimezoneOffset does not work all the time -


i register following javascript on web page. when user posts back, txttimezoneoffset hidden field comes blank. when test on computer, value. happens on different browsers (i.e, firefox, , safari).

i trying users offset, when enter date, can convert utc on server , deal in same timezone, when render dates out them, make sure in timezone.

<script type='text/javascript'>       $(window).load(function () {             var d = new date();             var tz = d.gettimezoneoffset();              $('#txttimezoneoffset').val(tz / 60);         }); </script> 

does see why not work 100% of time?

the jquery looks fine me, don't see why wouldn't work unless javascript disabled or didn't load jquery.

however, worked on flawed assumption user's current offset same dates. in time zone has daylight saving time, wrong.

the better approach convert utc on browser. or @ least use date/time in question in call gettimezoneoffset.

updated answer

in response question, code have current offset already. not guaranteed same offset date might enter. example:

<input type="text" id="somedate" /> 
var somedatestring = $('#somedate').val(); var dt = new date(somedatestring); var offset = dt.gettimezoneoffset(); 

since value of text input variable, in many time zones different offset january june. better thing convert utc on browser:

var somedatestring = $('#somedate').val(); var dt = new date(somedatestring); var s = dt.toisostring(); 

the string s in iso8601 format, , converted utc.

of course, if me using moment.js - since gives better control of how input string parsed.

this approach enough. however, if there chance have conversion on server, perhaps because user not present, must know time zone. not offset, actual time zone. see "time zone != offset" in timezone tag wiki.

the best way ask them it. map-based picker this one nice touch. can guess @ default selection using jstimezonedetect. after string identifier america/new_york can use on server. in .net can use these tzdb provider in noda time.

if don't want fancy libraries, can stick windows time zones, , present dropdown list timezoneinfo.getsystemtimezones(), .id key , .displayname value.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -