TypeError: moment(...).tz is not a function

12,054

Moment Timezone is a separate module, that needs to be installed in addition to moment. See the document to see how to install it.

Demo without tz module:

moment().tz();
<script src="https://momentjs.com/downloads/moment.js"></script>
</script>

Demo with tz module:

moment().tz();
<script src="https://momentjs.com/downloads/moment.js"></script>
<script src="https://momentjs.com/downloads/moment-timezone.min.js"></script>
Share:
12,054

Related videos on Youtube

Elad Benda
Author by

Elad Benda

linkedin

Updated on June 29, 2022

Comments

  • Elad Benda
    Elad Benda almost 2 years

    I have this js code:

    toolFilters.filter('dateAccordingToTimeZone', function($filter) {
        // Gets the number of milliseconds pass from 1970 and convert it to time according to given timezone, currently
        // supported “Asia/Jerusalem” and “America/Los_Angeles”
        return function(milliSeconds, timeZoneId) {
            if (milliSeconds == 0) {
                return "";
            }
    
            return moment().tz(milliSeconds, timeZoneId).format('MMM d, y H:mm:ss z');  // 5am PDT
    
        };
    })
    

    and this html

    <script src="bower_components/moment/moment.js"></script>
    

    after installing via bower

    but i get this error:

    VM9216:1 Uncaught TypeError: moment(...).tz is not a function
        at eval (eval at <anonymous> (filters.js:31), <anonymous>:1:10)
        at filters.js:31
        at fn (eval at compile (angular.js:15551), <anonymous>:4:485)
        at regularInterceptedExpression (angular.js:16658)
        at expressionInputsWatch (angular.js:16579)
    

    how can i fix this?

    as I based on the official doc

    just without the "require" as I use native js

    moment().format();
    • Elad Benda
      Elad Benda about 6 years
      I followed the docs. that's why i'm asking
  • Philipp Beck
    Philipp Beck about 6 years
    Okay the you need to import it in addition to moment, will edit my answer.
  • Elad Benda
    Elad Benda about 6 years
    It fails parsing my tz format even though it was supposed to support according to documentation stackoverflow.com/questions/50088484/…
  • Kevz
    Kevz about 2 years
    Running code snippet gives a moment(...).tz is not a function error in console log