Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select default timezone in moments.js

I include moments.js in the following way

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

I then give the timezone selection to my user in the html as

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

My question is how can I select a default timezone? I saw that one can do that with

moment.tz.setDefault(String);

but I don't understand how this would work in my setup?

EDIT: My implementation is just following this example...

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

does really nobody know a solution to this? carl

EDIT: I should clarify why moment.tz.setDefault does not work. When I add the line

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

I get the javascript error

Uncaught TypeError: Cannot read property 'setDefault' of undefined

I include moments as explained in the flask mega-tutorial meaning 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 %}

moment-timezones is included with the js file as shown above. Have I forgotten anything? I am just trying to set the default timezone for the selection

like image 428
carl Avatar asked Sep 26 '22 23:09

carl


1 Answers

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

It is also using old versions of moment-timezone ( which it is pulling in at that bottom of the timezones.full.js file, version 0.2.4) and the moment.tz.setDefault() wasn't added until a later version. The most recent as of now is 0.4.0. So, I would recommend mashing up your own files with the most recent versions, and try it all again on your system to see if it works.

UPDATE:

So, I looked at your fiddle, and updated it at your fiddle allowing for

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

with my own mashed up moment.js, moment-timezone.js, and jquery plugin code, and it works just fine. Basically all that moment-timezone-full.js file is doing is mashing up the moment.js and moment-timezone.js with 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 as well. I did notice that it will only pick up the first timezone in the list for that offset, not the actual one you specify, but that is a bigger issue with the moment-timezone.js library and outside of the scope of this question.

like image 170
irnmn Avatar answered Oct 18 '22 15:10

irnmn