Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to customize fromNow output ? #25

Closed
MrBillium opened this issue Oct 27, 2016 · 6 comments
Closed

How to customize fromNow output ? #25

MrBillium opened this issue Oct 27, 2016 · 6 comments
Assignees

Comments

@MrBillium
Copy link

I am displaying time data was received using flask Moment.fromNow. Typically it outputs 'a few seconds ago' but sometimes the sender's clock is a little off and it displays ' in a few seconds' - which makes no sense to the user.
To fix this I want to change ' in a few seconds' to 'current' but this does not seem easy using Moment customization so I thought I could simply change the output in jinja2 but cannot see how to capture the output string to change it:
Code:
{% set moment_text = moment(value.lastupdated).fromNow(no_suffix = False) %}

    {% if moment_text == "in a few seconds"  %}
        {% set moment_text = "current" %}
    {% endif %}

This never fires because moment_text is still a date and not a string.
How can I get the output string of Flask Moment so I can test it and change it?
Thanks
Bill

@miguelgrinberg
Copy link
Owner

This is a tricky issue to handle. The problem is that the timestamps are generated using the server's clock, but moment renders them using the clock on the client.

The typical solution is for either the client or the server to figure out what is the time difference with the other party, and then adjust the time so that the client always has it relative to its own clock. This could be an interesting feature enhancement for this extension, actually, but right now there is no support to do something like this.

You could have the client send its clock to the server via an ajax call when the page loads, and that will enable the server to adjust all timestamps for that client.

The solution that you propose can be implemented if you were working directly with moment in JavaScript, but all that is handled by Flask-Moment, so you have no easy access, unfortunately.

@MrBillium
Copy link
Author

I was hoping there was a way to cause Flask Moment to output the final string into another variable
something like {{ Set moment_textoutput = moment_text.to_string() }}

Then I would have a text variable that I could manipulate

@miguelgrinberg
Copy link
Owner

Flask-Moment does not generate the formatted date. All Flask-Moment does is generate the right JavaScript to render the date in the client. The time adjustment can be done in the server before rendering the template, but for that you need the client to have passed its current time in advance, so that the server can calculate an estimated time difference. It can also be done in the client if the server passes its time to it, so then the client calculates the difference and adjusts the time before passing it to moment.js. Unfortunately this last option can be implemented only if you write your own JavaScript, not if you let Flask-Moment autogenerate the JS.

@valentin-ballester
Copy link

valentin-ballester commented Jun 7, 2019

Hello, im having the same problem. cant the flask moment from_now being considered as a bug ? because i only dicovered this problem after many testings, and it could get my users lost. for the moment i disabled the subfix and i add it manually, it solves the problem but it is kinda hacking and i lost the possibility of using the full capability of moment, if i decided in the future to add a feature to schedule my orders, and i want moment to detect what subfix to use ? i cant because of this bug, i probably should the ajax alternative or i dont know what other alternatives i have.

edit: i know probably the word bug isnt the best to describe this, this is more a unwanted known problem that exists in the tiny differences of time between the server and the local pc.

@miguelgrinberg
Copy link
Owner

@valentin-ballester why do you attribute the bug to Flask-Moment? This problem is very common and affects any two systems that have clocks out of sync. If you generate a time in one of the system (server) and then pass it to the other (client) for rendering you are bound to have problems. It has nothing to do with Flask-Moment.

@miguelgrinberg
Copy link
Owner

I don't believe I'll ever have time to dedicate to this issue, which is extremely complex, so for now I'm going to close. The recommendation is that you make sure that the clocks are relatively close. If you want to make sure "in a few seconds" does not appear you can always add a few seconds to the times that you render.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants