Momentjs in wp doesn't work

Hello everyone,

I am trying to get momentjs working on wp but it keeps telling me is undefined and no function.
Could somebody check what is wrong with the code ?

jQuery(document).ready(function() {
	    jQuery.Deferred(function( deferred ){
        	jQuery( deferred.resolve );

    	var i ="2017-07-07 23:59", "Europe/Amsterdam");
    	jQuery("#dutchTimer").countdown(i.toDate(), function(t) {
    		jQuery(this).html(t.strftime("%D dagen %H:%M:%S"));

Best regards.

The error you’re seeing is because the scripts are being loaded in the wrong order. You need to load Moment before loading Moment Timezone and due to the nature of $.when(), this isn’t happening. Is there any reason you’re using $.when(), BTW?

The simplest way to accomplish what you want is:

<script src=""></script>
<script src=""></script>
<script src=""></script>
  console.log('2017-07-07 23:59', 'Europe/Amsterdam'))

Gives you:

Moment {_isAMomentObject: true, _i: "2017-07-07 23:59", _f: "YYYY-MM-DD HH:mm", _isUTC: true, _pf: Object…}
1 Like

well I did not want to hack in wp so tried it like that.
Afterwards changed it to that and it worked.

Well the “wordpress way” would be to register and enqueue them in your theme’s functions.php (or plugin entry PHP script, if that’s where they’re needed); this way you can specify which scripts depend on each other, and let WP ensure that all scripts are available when they’re needed.

Usually you can perfectly get away with just hard-coding the links at the bottom of the body, though. :-)

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.