0

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.

  • Please post the actual error. – PApostol Oct 05 '20 at 10:25
  • The actual error is as follow: ValueError at /event Wrong datetime format: 2020-10-07T22:28 Request Method: POST Request URL: http://127.0.0.1:8000/event Django Version: 3.1.1 Exception Type: ValueError Exception Value: Wrong datetime format: 2020-10-07T22:28 – Sebastian C Oct 06 '20 at 11:29

1 Answers1

0

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
Community
  • 1
  • 1
anmari
  • 3,830
  • 1
  • 15
  • 15
  • Thanks, @anmari, with your suggestion I solved the issue. I did the following: 1. I applied datetime.strptime to my current datetime startdate = datetime.strptime(startdate, "%Y-%m-%dT%H:%M") 2. I formated the field with one of the RFC5545 formats startdate = startdate.strftime("%Y%m%dT%H%M%S") – Sebastian C Oct 07 '20 at 08:06