0

We have a web app(Django based backend and Node/ReactJs based front-end, MySQL based DB), and a Flutter based mobile app. We are creating a system where a vendor can create a meeting invite and list the time (and timezone) at which the meeting starts. Visitors for that meeting should be able to see the time (and timezone) in their local time.

We will be having visitors' location and timezone ID based on their IP.

I can't figure out how to display the list of timezones(preferably in the GMT+xxxx format ) in the front-end for the vendor to choose when he creates an invite and in what format it should be sent to the backend and stored so that I am able to convert it and display it to the visiting user based on his location's timezone?

I would be really grateful if someone here can point me to the right question if it has been asked already or some document from which I can refer to for the implementation?

HARSHIT BAJPAI
  • 361
  • 2
  • 17

2 Answers2

0

You can use pytz for creating a list of timezones.

There's documentation in Django stating how to select current timezone and creating a list of timezones link


Create a list of timezone select option

# views.py
import pytz

def your_views(request):
    # code
    return render(request, 'your_template.html', {'timezones': pytz.common_timezones})

In your_template.html file

<select name="timezone">
    {% for tz in timezones %}
    <option value="{{ tz }}">{{ tz }}</option>
    {% endfor %}
</select>
Achuth Varghese
  • 2,356
  • 1
  • 4
  • 18
  • Is there a way to list all the timezones in this? How will I display it in frontend and what should be the format of response sent from the frontend? – HARSHIT BAJPAI Aug 28 '20 at 10:44
  • @HARSHITBAJPAI yes there is. `import pytz` and pass `{'timezones': pytz.common_timezones}` in context. You can loop through this to create a select option list for timezone selection. In template.html provided in docs you can see how the context has been implemented to create a select option list – Achuth Varghese Aug 28 '20 at 10:47
0

Normal people don't like GMT+X. Second, basing a timezone on IP is irritating for people using VPNs, especially since the browser stores a timezone based on computer settings. Granted, it takes a request to be sent via javascript, so the first page won't know what the timezone is. See Intl.DateTimeFormat.resolvedOptions().timeZone.

To generate choices that correspond with that list, use pytz.common_timezones:

import pytz

class YourModel(models.Model):
    TZ_CHOICES = ((v, v) for v in pytz.common_timezones)
    time_zone = models.CharField(choices=TZ_CHOICES)