1

in my website a user saves DateTimeField in model and I need to display this time in my template according to other user's local time. For instance, if a user in Mexico creates a new tour it should be saved in model in Mexico time but for a user in America i want it to be displayed in US time.

This is my setting.py :

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

and this is my code in views.py :

utc = pytz.utc
date_field = form.cleaned_data['date']
time_field = form.cleaned_data['time']
meeting.date_time = utc.localize(datetime.combine(date_field,time_field))
meeting.save()

and this is my template :

  <p class="">{{meeting.date}} {{meeting.date_time|localtime|date:'h:i A'}}</p>

but it is not working!

Ralf
  • 14,947
  • 4
  • 39
  • 61
lotus
  • 31
  • 5
  • 1
    It's usually easier to do this in your frontend code - i.e. ask the browser to convert a given timestamp to the user's local time. https://stackoverflow.com/a/31453408/137650 – MatsLindh Jan 22 '21 at 13:55

0 Answers0