0

When I change the timezone in my django project the dates do change correctly. When the timezone is set to 'UTC' the dates are displayed like this:

enter image description here

This is correct. As you can see these are also the dates which are specified in my database:

enter image description here

But when I change the settings to:

LANGUAGE_CODE = 'nl'
TIME_ZONE = 'Europe/Amsterdam'
USE_I18N = True
USE_L10N = True
USE_TZ = True

The dates inside my application now change to the image below. Only the first two dates are displayed in the correct way, even though nothing has changed except for the timezone.

enter image description here

Does anyone know why this happens, and how I can solve this?

EDIT: Code

def afspraak_maken(request):    
    form = EventForm(request.POST or None)
    if request.POST and form.is_valid():
        title = form.cleaned_data['title']
        soort = form.cleaned_data['soort']
        description = form.cleaned_data['description']
        start_time = form.cleaned_data['start_time']
            
        Event.objects.get_or_create(
            user=request.user,
            title=title,
            soort=soort,
            description=description,
            start_time=start_time,
            time_set = time_set
        )
            
        Timing.objects.get_or_create(
                event_id=find_id,
                name= 1,
                start_time=localtime(start_time),
                end_time=localtime(start_time) + timedelta(hours=1, minutes=30)
            )

        Timing.objects.get_or_create(
            event_id=find_id,
            name= 2,
            start_time=  localtime(start_time) + timedelta(days=7),
            end_time= localtime(start_time) + timedelta(days=7,hours=1, minutes=30)
            
        )

        Timing.objects.get_or_create(
            event_id=find_id,
            name= 3,
            start_time=localtime(start_time) + timedelta(days=21),
            end_time=localtime(start_time) + timedelta(days=21,hours=1, minutes=30)
        )
        return redirect('afspraak_bewerken', pk=find_id)

    return render(request, 'kalender/afspraak_maken.html', {'form': form})
                       

My Timing model:

class Timing(models.Model):
     event = models.ForeignKey(Event, null=True, on_delete=models.CASCADE)
     name = models.CharField(max_length=200)
     start_time = models.DateTimeField()
     end_time = models.DateTimeField()
     comment = models.CharField(max_length=300,null=True, blank=True)

One of the tables where I display my dates

<table class="homepage-table mt-2 mb-2" id="myTable2">
      <tr>
        <th> Afspraak</th>
        <th> Training</th>
        <th> Start tijd</th>
        <th> Eind tijd</th>
        <th> Locatie</th>
        <th> Trainer</th>
      </tr>

      {% for timing in timing%}
      <tr>

        <td> {{timing.name}}</td>
        <td> {{timing.event.title}}</td>
        <td> {{timing.start_time|date:'d-m-Y H:i'}}</td>
        <td> {{timing.end_time|date:'d-m-Y H:i'}}</td>
        <td> {{timing.event.locatie}}</td>
        <td> {{timing.event.trainer}}</td>
       
      </tr>
      {% endfor%}
    </table>

1 Answer 1

2

There is no problem in your display. But in Nl, the 28th of march 2021 there is a time change that's why the 2 first seems "at the good time" and not the others.

So the 20-03-2021, 11:00 UTC = 20-03-2021 12:00 in NL (UTC+1) But the 04-04-2021, 11:00 UTC = 04-04-2021 13:00 in NL (UTC+2)

You can try this:

from django.utils.timezone import localtime

new_start_date = localtime(start_date) + timedelta(days=7)
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for pointing that out. I would never have come to that conclusion. So my problem here is that it sends the dates in UTC to the database when UTC difference is +1, but it returns +2 since the date that I have set is after the 28th of March. Do you know how I can make sure that I see all the dates in the current time conversion instead of the future time conversion?
The problem is that you changed the timezone after saving your data. If you have the good TZ when you save your data it will display as you want. The best to do now is to delete these entries then recreate them with your TZ set in the settings and not changing it again
It still does this for new projects. I think the problem is that I try to add 7 days to my dates by using timedelta(days=7). In 7 days the date is indeed going to be an hour later due to DST, but that is not what I want to show.
Oh ok you add it like that, in fact you probably use a naive delta. so try with the timezone one: timezone.now() + timezone.timedelta(days=7)
This still adds the hours. Ideally there would be a solution which makes sure that the UTC time in the database is set to one hour less after the DST. But this does not seem to happen...
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.