0

I am creating a microsoft graph event object using this code:

Event @event = new Event
{
  Subject = subject,
  Body = new ItemBody
  {
    ContentType = BodyType.Html,
    Content = $"<div id=\"BookingId\">{ id }</div><div id=\"CheckSum\">{ checkSum }</div>"
  },
  Start = new DateTimeTimeZone
  {
    DateTime = begin.ToString("yyyy-MM-ddTHH:mm:ss", new CultureInfo("de-AT")),
    TimeZone = TimeZoneInfo.Local.Id
    },
    End = new DateTimeTimeZone
    {
        DateTime = end.ToString("yyyy-MM-ddTHH:mm:ss", new CultureInfo("de-AT")),
        TimeZone = TimeZoneInfo.Local.Id
    },
    Location = new Location
    {
        DisplayName = location
    },
    Attendees = new List<Attendee>(),
    IsAllDay = true
};

TimeZone is set to "W. Europe Standard Time" when this code runs on my machine. Events will be added like this:

await graphClient.Groups[group.Id].Events
   .Request()
   .AddAsync(@event);

This works so far but all my events have one day less than I originally set for the end time. Also tried to add some hours but then I get an exception saying that allDayEvents have to start and end at midnight. But setting them to midnight removes the last day of the event.

The easiest workaround seems to be always adding one day to the end date. But what if this is only a daylight savings problem and I will then get one day to much in winter? I did not find any possibility to add the daylight saving hour to the start and end date.

Additional information: The datetime objects I am using in the code above are already having "00:00:00" as the time. This can also be seen from the text above as there will be an exception when using a different time. Therefore the answer to the linked question did not solve my problem. So, no need to vote down my question.

DrDrakken
  • 37
  • 7
  • Midnight **today** is basically the end of yesterday. Add one day to the end date. – mjwills Jun 17 '19 at 13:53
  • Another way to think of it - let's say the meeting was the entirety of today (and just today). What should the start be? End be? – mjwills Jun 17 '19 at 13:54
  • When adding a all-day event via code the office-365-outlook calendar shows an end time of 23:59:59. And not 00:00:00 of the next day. Why ever. So, I will try to add one day to the end date and hope, it will also work in winter :) – DrDrakken Jun 18 '19 at 07:53

1 Answers1

1

When creating an "All Day" event, you start and end times should only specify the date, not the date and time (or more accurately, the time should be 00:00:00):

Start = new DateTimeTimeZone
{
    DateTime = begin..ToString("yyyy-MM-ddT00:00:00"),
    TimeZone = TimeZoneInfo.Local.Id
},
End = new DateTimeTimeZone
{
    DateTime = end.ToString("yyyy-MM-ddT00:00:00"),
    TimeZone = TimeZoneInfo.Local.Id
},

Very similar to this question: Creating All Day Event failing

Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
  • Thanks for your answer. My dates are already having date only, so time is 00:00:00. And my question might be similar to the question you linked to but this did not solve my problem. – DrDrakken Jun 18 '19 at 07:48
  • 1
    @MarcLaFleur If an allday event is one day, then the start and end date time will be the same, right. Then it responses an error, "your request can't be completed. The duration of an event marked as All day must be at least 24 hours". How to fix this? – hotcakedev Mar 16 '21 at 17:14
  • @hotcakedev You have to add one day to the end date: endDate.AddDays(1), because an allday event runs from midnight this day to midnight next day – Beetee Jan 06 '23 at 10:03