Select the default time zone in moments. js

I include .js moments as follows

<script src="../static/js/moment-timezone-full.js"></script> <script> $('.select_timezone').timezones(); </script> 

Then I give the time zone choice to my user in html like

 <div class="row"> <div class="col-lg-4"> <select class="select_timezone" name="select_timezone"></select> </div> </div> 

My question is, how can I choose the default time zone? I saw that this can be done with

 moment.tz.setDefault(String); 

but I don’t understand how this will work in my setup?

EDIT: my implementation is only after this example ...

http://www.jqueryscript.net/time-clock/Easy-Timezone-Picker-with-jQuery-Moment-js-Timezones.html

Does anyone really know a solution to this? carl

EDIT: I have to clarify why moment.tz.setDefault is not working. When i add the line

 moment.tz.setDefault('America/Los_Angeles'); 

I get javascript error

 Uncaught TypeError: Cannot read property 'setDefault' of undefined 

I include moments, as explained in the mega-flask tutorial, what I do

 from flask.ext.moment import Moment ... moment = Moment() ... moment.init_app(app) 

and in my html template i do

 {% block scripts %} {{ moment.include_moment() }} {% endblock %} 

instant clocks are included in the js file as shown above. Did I forget something? I'm just trying to set the default timezone for selection

+5
source share
2 answers

After seeing what you posted, it looks like you are using the jquery plugin from https://github.com/firstandthird/timezones , which when I look at the code using an older version of moment.js (version 2.8.3) as shown at https://github.com/firstandthird/timezones/blob/master/dist/timezones.full.js

It also uses older versions of moment-time (which it draws to the bottom of the file timezones.full.js, version 0.2.4), and the parameter moment.tz.setDefault () was not added until a later version. The most recent at the moment is 0.4. Thus, I would recommend to stretch your own files with the latest versions and try all this again on your system to see if it works.

UPDATE:

So, I looked at your violin and updated it to my violin , allowing

 moment.tz.setDefault("America/New_York"); $('.select_timezone').timezones(); 

with my own mashed up.js, moment-timezone.js and jquery plugin code, and it works great. Basically, all that the time-timezone-full.js file does is overwrite the .js moment and the moment-timezone.js using the jquery plugin code. (it is listed as one of the external libraries, you can find it at https://gist.github.com/iron-man/deedd9efe988318d842a . I noticed that he only picks up the first time zone in the list for this offset, and not the one which you pointed out, but this is a big problem with the moment-timezone.js library and beyond the scope of this question.

+3
source

By default, it displays the time zone of the person who is looking at it, but you can override it by defining it. In your example, you can do something like:

 <script src="../static/js/moment-timezone-full.js"></script> <script> moment.tz.setDefault("America/New_York"); $('.select_timezone').timezones(); </script> 
+1
source

All Articles