Search code examples
javascriptanytime

Set Timezone offset programatically in AnyTime datepicker


I've been using AnyTime datepicker: http://www.ama3.com/anytime/ and I now need to set the TimeZone and update AnyTime datepicker with javascript. Only I can't seem to find the option to do this, And I'm not exactly a Javascript Hero.

I think I've narrowed it down to the function askOffset: function( event ) on line 1919 But can't exactly figure it out what to do next. I don't know what the abbreviated variables mean and I've been randomly trying things and can't figure it out.

The reason I'm doing this is because I have the select dropdown somewhere else on the screen (looks fancier) and I also want the ability to load the saved timezone from cookies.

I've also found some documentation that might be useful: utcFormatOffsetImposed and utcParseOffsetAssumed on the AnyTime page.


Solution

  • The picker automatically parses the timezone from the value in the input field, so if you want to set a specific timezone, just initialize the input field with a value that uses the same timezone.

    utcFormatOffsetImposed and utcParseOffetAssumed can be used to force specific time zone conversion in AnyTime.Converter, so if you want to use the converter to initialize the field, you can do something like this:

    <input type="text" id="myField"/>
    <script>
    $(function(){
    var myFormat = '%r %@';
    var myConv = new AnyTime.Converter({format:myFormat});
    myConv.utcFormatOffsetImposed( -330 );
    $('#myField').
      AnyTime_picker({format:myFormat}).
      val( myConv.format(new Date()) );
    });
    </script>
    

    Of course, replace the format string with whatever you want, as long as it contains %#, %+, %-, %;, %; or %@ for the timezone information.