API Reference

class moment(timestamp=None, local=False)

Create a moment object.

Parameters:
  • timestamp – The datetime object representing the timestamp.

  • local – If True, the timestamp argument is given in the local client time. In most cases this argument will be set to False and all the timestamps managed by the server will be in the UTC timezone.

classmethod include_moment(version='2.29.4', local_js=None, no_js=None, sri=None, with_locales=True)

Include the moment.js library and the supporting JavaScript code used by this extension.

This function must be called in the <head> section of the Jinja template(s) that use this extension.

Parameters:
  • version – The version of moment.js to include.

  • local_js – The URL to import the moment.js library from. Use this option to import the library from a locally hosted file.

  • no_js – Just add the supporting code for this extension, without importing the moment.js library. . Use this option if the library is imported elsewhere in the template. The supporting JavaScript code for this extension is still included.

  • sri – The SRI hash to use when importing the moment.js library, or None if the SRI hash is unknown or disabled.

  • with_locales – If True, include the version of moment.js that has all the locales.

static locale(language='en', auto_detect=False, customization=None)

Configure the moment.js locale.

Parameters:
  • language – The language code.

  • auto_detect – If True, detect the locale from the browser.

  • customization – A dictionary with custom options for the locale, as needed by the moment.js library.

static flask_moment_js()

Return the JavaScript supporting code for this extension.

This method is provided to enable custom configurations that are not supported by include_moment. The return value of this method is a string with raw JavaScript code. This code can be added to your own <script> tag in a template file:

<script>
    {{ moment.flask_moment_js() }}
</script>

Alternatively, the code can be returned in a JavaScript endpoint that can be loaded from the HTML file as an external resource:

@app.route('/flask-moment.js')
def flask_moment_js():
    return (moment.flask_moment_js(), 200,
        {'Content-Type': 'application/javascript'})

Note: only the code specific to Flask-Moment is included. When using this method, you must include the moment.js library separately.

static lang(language)

Set the language. This is a simpler version of the locale() function.

Parameters:

language – The language code to use.

format(fmt=None, refresh=False)

Format a moment object with a custom formatting string.

Parameters:
  • fmt – The formatting specification to use, as documented by the format() function frommoment.js.

  • refresh – If set to True, refresh the timestamp at one minute intervals. If set to False, background refreshing is disabled. If set to an integer, the refresh occurs at the indicated interval, given in minutes.

fromNow(no_suffix=False, refresh=False)

Render the moment object as a relative time.

This formatting option is often called “time ago”, since it renders the timestamp using friendly text strings such as “2 hours ago” or “in 3 weeks”.

Parameters:
  • no_suffix – if set to True, the time difference does not include the suffix (the “ago” or similar).

  • refresh – If set to True, refresh the timestamp at one minute intervals. If set to False, background refreshing is disabled. If set to an integer, the refresh occurs at the indicated interval, given in minutes.

fromTime(timestamp, no_suffix=False, refresh=False)

Render the moment object as a relative time with respect to a given reference time.

This function maps to the from() function from moment.js.

Parameters:
  • timestamp – The reference datetime object.

  • no_suffix – if set to True, the time difference does not include the suffix (the “ago” or similar).

  • refresh – If set to True, refresh the timestamp at one minute intervals. If set to False, background refreshing is disabled. If set to an integer, the refresh occurs at the indicated interval, given in minutes.

toNow(no_suffix=False, refresh=False)

Render the moment object as a relative time.

This function renders as the reverse time interval of fromNow().

Parameters:
  • no_suffix – if set to True, the time difference does not include the suffix (the “ago” or similar).

  • refresh – If set to True, refresh the timestamp at one minute intervals. If set to False, background refreshing is disabled. If set to an integer, the refresh occurs at the indicated interval, given in minutes.

toTime(timestamp, no_suffix=False, refresh=False)

Render the moment object as a relative time with respect to a given reference time.

This function maps to the to() function from moment.js.

Parameters:
  • timestamp – The reference datetime object.

  • no_suffix – if set to True, the time difference does not include the suffix (the “ago” or similar).

  • refresh – If set to True, refresh the timestamp at one minute intervals. If set to False, background refreshing is disabled. If set to an integer, the refresh occurs at the indicated interval, given in minutes.

calendar(refresh=False)

Render the moment object as a relative time, either to current time or a given reference timestamp.

This function renders relative time using day references such as tomorrow, next Sunday, etc.

Parameters:

refresh – If set to True, refresh the timestamp at one minute intervals. If set to False, background refreshing is disabled. If set to an integer, the refresh occurs at the indicated interval, given in minutes.

valueOf(refresh=False)

Render the moment object as milliseconds from Unix Epoch.

Parameters:

refresh – If set to True, refresh the timestamp at one minute intervals. If set to False, background refreshing is disabled. If set to an integer, the refresh occurs at the indicated interval, given in minutes.

unix(refresh=False)

Render the moment object as seconds from Unix Epoch.

Parameters:

refresh – If set to True, refresh the timestamp at one minute intervals. If set to False, background refreshing is disabled. If set to an integer, the refresh occurs at the indicated interval, given in minutes.

diff(timestamp, units, refresh=False)

Render the difference between the moment object and the given timestamp using the provided units.

Parameters:
  • timestamp – The reference datetime object.

  • units – A time unit such as years, months, weeks, days, hours, minutes or seconds.

  • refresh – If set to True, refresh the timestamp at one minute intervals. If set to False, background refreshing is disabled. If set to an integer, the refresh occurs at the indicated interval, given in minutes.