Django icalendar dtstart datetime issue

青春壹個敷衍的年華 提交于 2021-01-29 12:31:14

问题


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.


回答1:


Reformat the date times into one of the RFC5545 formats. Please see the RFC5545 specifications instructions for the datetime formats: https://tools.ietf.org/html/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


来源:https://stackoverflow.com/questions/64203439/django-icalendar-dtstart-datetime-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!