datetimeserializationicalendarutc

Ical.net Serialization Error when serializing 12:00 AM Midnight


Using ICal.net, I serialize a calendar using this code:

var calendarSerializer = new CalendarSerializer();
var attachmentContent = calendarSerializer.SerializeToString(myCalendar);

When I serialize a DateTime that's defined in UTC, 11:00 PM for example, I get this:

20240330T230000Z

When I serialize 12:00 AM Midnight, I get this:

VALUE=DATE:20240331

Which is confusing MS-Outlook when I import my calendar.

I need Midnight to serialize like any other DateTime, and I need it to include the timezone, so that it converts to local time correctly.


Solution

  • Here's the code I use to create my calendar object:

    var calendar = new Calendar
    {
        Method = "PUBLISH",
        ProductId = "MyProduct 6/8",
        Version = "1.0"
    };
    
    var calendarEvent = new CalendarEvent
    {
        Sequence = 1,
        Url = new Uri(location),
        Uid = eventId.ToString(),
        Description = null,     // details added as property
        Summary = eventName,
        Location = location,
        Start = new CalDateTime(startDateTimeUtc, "UTC"),
        End = new CalDateTime(endDateTimeUtc, "UTC")
    };
    

    adding this:

    calendarEvent.Start.HasTime = true;
    calendarEvent.End.HasTime = true;
    

    forces the serializer to guarantee the presence of a time and a time zone, even if it's just "000000Z". This corrects the issues I was having when importing into MS-Outlook calendar.