5

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

carl
  • 4,216
  • 9
  • 55
  • 103
  • Could you specify what you mean by "_following this example_"? Do you plan to use __Timezones__ jQuery plugin to draw a selection box? Or do you want to make your own implementation similar to __Timezones__ plugin? – Konstantin Grushetsky Aug 31 '15 at 09:20
  • Also note that `moment.tz.setDefault` method you indicated in the question is a proper way for setting the default timezone with __Moment Timezone__ library. So, what's the real problem here? – Konstantin Grushetsky Aug 31 '15 at 09:27
  • @carl Just add `onchange` to your select element and get the value using javascript and set the default timezone using `moment.tz.setDefault`. What's wrong? – Mingle Li Aug 31 '15 at 14:25
  • 1
    Please clarify - Are you trying to configure the default time zone of moment.js? Or are you trying to set the default selected time zone entry of your drop-down list? They are two very different things. – Matt Johnson-Pint Aug 31 '15 at 18:14
  • I added some more explanation above for why the moment.tz.setDefault function does not work for me. thanks for your help – carl Aug 31 '15 at 20:15

2 Answers2

3

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.

irnmn
  • 726
  • 4
  • 20
  • I updated the versions to 2.10.6 for moment.js and 0.4.0 for moment-timezone. unfortunetely it did not solve the problem. The error message is still "Uncaught TypeError: moment.tz.setDefault is not a function" – carl Sep 01 '15 at 15:59
  • did you update the moment-timezones-full.js file? that is the one that is bringin in the outdated versions. Can you check the versions to be sure what is being loaded by dropping in a console.log and inspecting the js file that is responding, it should have the version in the comments of the file. – irnmn Sep 01 '15 at 21:00
  • yes I updated the versions in moment-timezones-full.js... printing out the versions confirms that I now use moment.tz.version = 0.4.0, moment.version = 2.10.6... – carl Sep 01 '15 at 22:04
  • ok that works thanks a lot for your patience... one has to do a lot of changes to the moment-timezone file so for anybody wanting to do the same, just use the file provided above by irnmn... thanks again – carl Sep 02 '15 at 01:13
1

By default it displays the timezone of the person looking at it, but you can override that by specifically defining it. In your example you could do something like:

<script src="../static/js/moment-timezone-full.js"></script>
<script>
  moment.tz.setDefault("America/New_York");
  $('.select_timezone').timezones();
</script>
Konstantin Grushetsky
  • 1,012
  • 1
  • 15
  • 32
irnmn
  • 726
  • 4
  • 20
  • hi irnmn, thanks for your answer. I added some text above to explain the error I get when using this approach. – carl Aug 31 '15 at 20:15
  • @carl You can try doing it a different way, just setting the timezone manually, instead of setting the default. Seems like there are some good details here [link](http://stackoverflow.com/questions/15347589/moment-js-format-date-in-a-specific-timezone?rq=1). This is assuming that is what you are trying to do. As someone else commented, are you trying to just set the default option in the select menu, or set the timezone for all entries? – irnmn Aug 31 '15 at 20:31
  • I am just trying to set the default timezone for the selection – carl Aug 31 '15 at 20:36
  • I assume you are loading in some default data from one of the pre-built moment-timezone downloads, or are you building the data yourself with a call to .load()? They have a number of different builds, are you using one of their 2014i builds? If you are, they should have the moment.tz.setDefault command in there for you to use. Did you try using the moment.tz.setDefault right after your moment.init_app(app)? – irnmn Aug 31 '15 at 20:55
  • the moment.init_app(app) is only for the moment package. Currently I can't see how adding the timezone js file suddenly gives the .tz attribute to the moment function? if I use the moment function in my template with jinja2 (e.g. {{ moment(date).calendar() }}) works fine, but I don't use moment anywhere ouside jinja2... in particular I do not .load() anything but use a pre-build timezone download – carl Aug 31 '15 at 21:04
  • what is the order of how you are putting in your files, do you have the {{ moment.include_moment() }} part before or after the It would need to be before, so it is loaded first, and then the timezone plugin. Once they are both loaded on the page, you should have access to the moment.tz variable, as that should have been loaded to the page when you loaded the moment-timezone-full.js file. Can you check the inspector to be sure that it loaded correctly? The call to the moment.tz has to be in the page template, not in the python. – irnmn Aug 31 '15 at 23:26
  • yes I load the moment package first. Maybe it is easier if you could show me how to implement it in a fiddle? This setup is different to what I use, but it would be great to see a working version https://jsfiddle.net/bcop9wy5/3/ thanks carl – carl Sep 01 '15 at 01:06