首页 > 解决方案 > Django icalendar dtstart 日期时间问题

问题描述

我在 Django-python 中有一个用于事件程序的表单。我正在尝试使用 icalendar 为事件创建一个 ics 文件,为此,我想从表单中的变量“starttime”和“endtime”中获取值“dtstart”和“dtend”,但我得到了代码:错误的日期时间格式。任何人有任何建议来解决这个问题?

错误

            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.
    """

代码

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))

在此先感谢,我正在学习python,所以我没有太多经验。

标签: pythondjangoicalendar

解决方案


将日期时间重新格式化为 RFC5545 格式之一。请参阅日期时间格式的 RFC5545 规范说明:https ://www.rfc-editor.org/rfc/rfc5545#section-3.3.5 。

有 3 种可接受的日期时间格式:

  1. 本地或“浮动”,例如:19980118T230000
  2. 带有 UTC 时间的日期,例如:19980119T070000Z 和
  3. 带有本地时间和时区参考的日期,例如:TZID=America/New_York:19980119T020000

推荐阅读