Search code examples
pythondjangoicalendar

Django icalendar dtstart datetime issue


I have a form in Django-python for an event program. I'm trying to create an ics file for the events with icalendar, for this, I want to get the values 'dtstart' and 'dtend' from the variables 'starttime' and 'endtime' in the form, but I'm getting the code: Wrong datetime format. Anyone with any advice to solve this issue?

ERROR

            elif not ical[15:]:
                return datetime(*timetuple)
            elif ical[15:16] == 'Z':
                return pytz.utc.localize(datetime(*timetuple))
            else:
                raise ValueError(ical)
        except:
            raise ValueError('Wrong datetime format: %s' % ical) …
class vDuration(object):
    """Subclass of timedelta that renders itself in the iCalendar DURATION
    format.
    """

CODE

def event(request, id=None):
    instance = Event_cal()
    
    if id:
        instance = get_object_or_404(Event_cal, pk=id)
    else:
        instance = Event_cal()

    form = EventForm(request.POST or None, instance=instance)
    if request.POST and form.is_valid():
        form.save()
        
        startdate = request.POST.get('starttime')
        endate = request.POST.get('endtime')

        event = Event()
        event.add('summary', 'My Summary')
        event.add('dtstart', vDatetime.from_ical(startdate))
        event.add('dtend', vDatetime.from_ical(endate))

Thanks in advance, I am learning python, so I don't have have much experience.


Solution

  • Reformat the date times into one of the RFC5545 formats. Please see the RFC5545 specifications instructions for the datetime formats: https://www.rfc-editor.org/rfc/rfc5545#section-3.3.5.

    There are 3 accepted datetime formats:

    1. Local or 'floating' eg: 19980118T230000
    2. Date with UTC time eg: 19980119T070000Z and
    3. Date with local time and timezone reference eg: TZID=America/New_York:19980119T020000