By default, the DatePicker is displayed in my localized format.
However, a text label with data bound from the database is displayed in the format YYYY-MM-DD, and not accordingly to my browser.
Is there a way to display this date in a localized format?
(Chatbeaverbot is such a great help for me, a big thank you!
I rely on it so much, but in this case, it couldn’t assist me)
My form, a date picker and a label bound to a column in the database:
Have a look through the custom component that does this conversion to see the technique used there and adapt it to your use case: Local Timezone Display in Data Grids
# add this import to the form
import anvil.tz
# use this in the databinding
self.item['added'].astimezone(anvil.tz.tzlocal()).strftime("%Y-%m-%d %H:%M:%S")
@anvil.server.callable
def get_preferred_locale():
country = anvil.server.context.client.location.country
if country in ['Germany', 'Austria', 'Switzerland']:
return 'DE'
elif country in ['France', 'Belgium', 'Luxembourg']:
return 'FR'
elif country in ['Spain', 'Mexico', 'Argentina']:
return 'ES'
elif country in ['Italy']:
return 'IT'
elif country in ['Sweden']:
return 'SV'
elif country in ['Norway']:
return 'NO'
elif country in ['Greece']:
return 'EL'
else:
return 'EN'
@anvil.server.callable
def format_locale_datetime(myDatetime):
preferred_locale = anvil.server.call('get_preferred_locale')
if preferred_locale in ['FR', 'CA', 'BE', 'LU', 'CH_FR', 'ES', 'IT', 'EL']:
return myDatetime.strftime("%d/%m/%Y %H:%M")
elif preferred_locale in ['DE', 'CH', 'AT', 'NO']:
return myDatetime.strftime("%d.%m.%Y %H:%M")
elif preferred_locale in ['SV']:
return myDatetime.strftime("%Y-%m-%d %H:%M")
else: # English by default
return myDatetime.strftime("%b %d %Y %I:%M %p")
When you’re in a server function, don’t use anvil.server.call to call another server function, that just adds overhead. Just call the server function like a normal function, e.g.:
The client context isn’t going to change for a user during a session, so you could make that get_preferred_locale call once on startup and cache the value in a client module, and then use client code for the strftime calls, avoiding the overhead of the frequent server calls.
As is, there’s going to be a delay each time you set the text for a datetime because of the server call.
yes it’s useless to fetch locale things each time. Furthermore countries like Switzerland make it difficult, as there is not a single language.
I will rework it so that the user can choose his own locale settings in a parameter form.
If not set by the user, then use get_preferred_locale() and store it in my user table in database.
I noticed I also need to fix the date picker, and this will be very helpfull: