简体   繁体   中英

Calculate Google Calendar event duration in C#

I have been working on a C# app in Visual Studio 2013 that will calculate employee hours from Google Calendar events. I have used this link, https://developers.google.com/google-apps/calendar/quickstart/dotnet , for instructions on how to connect to Google Calendar via the API. When I enter my hours worked for the day on the calendar, I create an event, and use the From Time as the start time, and the Until Time as the end time. I can't figure out how to calculate the total hours for the day. What I want to be able to do is simply subtract the From time from the Until time to get the total hours worked for that day. I just can't figure out how to get the start and end times into variables that I can then perform arithmetic on. I can then figure out how to do that for each day of the week, and add them together.

Using example from Google Calendar API, they provide the event date & date time as below,

Events events = request.Execute();
Console.WriteLine("Upcoming events:");
if (events.Items != null && events.Items.Count > 0)
{
    foreach (var eventItem in events.Items)
    {
        string when = eventItem.Start.DateTime.ToString();//Right here
        if (String.IsNullOrEmpty(when))
        {
            when = eventItem.Start.Date;//And here
        }
        Console.WriteLine("{0} ({1})", eventItem.Summary, when);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM