django pass datetime to TempusDominus

28 views Asked by At

When I edit en entity I want to pass the datetime to the form, but the format is not respected. In my view I have:

    def get_initial(self):
        initial = {
            'season': Season.objects.get(pk=self.request.session['selected_season_id']),
            'start': self.object.start.strftime('%Y-%m-%d') if self.object.allday else self.object.start.strftime('%Y-%m-%d %H:%M')
    }
        if self.object.end is not None:
            initial = {
                'end': self.object.end.strftime('%Y-%m-%d') if self.object.allday else self.object.end.strftime('%Y-%m-%d %H:%M')
            }
        return initial

The form has following options:

var picker = new tempusDominus.TempusDominus(document.getElementById(date_element_id), {
                //put your config here
                
                localization: {
                    startOfTheWeek: 1,
                    format: 'yyyy-MM-dd HH:mm',
                    hourCycle: 'h24'
                },
                restrictions: {
                    minDate: new Date(),
                    maxDate: undefined,
                    disabledDates: [],
                    enabledDates: [],
                    daysOfWeekDisabled: [],
                    disabledTimeIntervals: [],
                    disabledHours: [],
                    enabledHours: [],

                }
            });

            picker.dates.formatInput = date => moment(date).format('yyyy-MM-DD HH:mm');

On an empty input field the datetime is shown correct. On an existing field, for an edit, the format is '12/31/2024, 16:00'

But the datetime-string is passed correctly to the form and in the source I also see the correct format like '2024-12-31 16:00' . Why is the format changed?

0

There are 0 answers