.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> DateTime >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Find Sum of Time Differences

Posted By:Jean Paul       Posted Date: October 31, 2011    Points: 40    Category: DateTime    URL: http://www.dotnetspark.com  

Find the sum of time differences for a list of date time. (SignIn and SignOut)
 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            IList list = new List();

            // login at 9am, team break at 10:30 am
            list.Add(new TimeEntry() { LoginTime = new DateTime(2011, 10, 31, 9, 0, 0), LogoutTime = new DateTime(2011, 10, 31, 10, 30, 0) });

            // login at 11am, lunch break at 1pm
            list.Add(new TimeEntry() { LoginTime = new DateTime(2011, 10, 31, 11, 0, 0), LogoutTime = new DateTime(2011, 10, 31, 13, 0, 0) });

            // login at 2pm, continuous work till 5pm
            list.Add(new TimeEntry() { LoginTime = new DateTime(2011, 10, 31, 14, 0, 0), LogoutTime = new DateTime(2011, 10, 31, 17, 0, 0) });

            // Calculate using loop
            double totalHours = 0;

            foreach (TimeEntry entry in list)
            {
                TimeSpan span = entry.LogoutTime - entry.LoginTime;
                totalHours += span.TotalHours;
            }

            // Display
            Console.WriteLine(string.Format("Total Time Worked: {0}", totalHours));
            Console.ReadKey(false);
        }
    }

    public class TimeEntry
    {
        public DateTime LoginTime;
        public DateTime LogoutTime;
    }
}


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend