Find # of hours between 2 dates, excluding weekends, in C#

I need to calculate the number of hours a ticket has been, or was open by subtracting 2 dates. All weekend hours need to be excluded. (All in C#) Can someone assist?
devo00Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jazzIIIloveCommented:
Hi there;

yes we can...any code snippet you can provide us?

Best regards...
0
jazzIIIloveCommented:
or algorithm?
0
devo00Author Commented:
Sure, so far I only display the flat difference in hours (see snippet). I need to adapt it to exclude weekends.

snippet.txt
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

nullcoryCommented:
Super quick, super dirty.  This calculates days but you could easily modify it to do hours.
I can modify it if you need.
            //Todays Date
            DateTime dt2 = DateTime.Now;
 
            //Previous Date
            DateTime dt = DateTime.Parse("10/15/2008");
 
            //TimeSpan Between Dates
            TimeSpan dt3 = dt2 - dt;
 
            //Count of days not including weekends
            int dayCount = 0;
 
            //1 day timespan
            TimeSpan ts = new TimeSpan(1, 0, 0, 0);
 
            for (int i = 1; i < dt3.Days+1; i++)
            {
                //Today - TimeSpan(1 Day)
                dt2 = dt2.Subtract(ts);
 
                if (dt2.DayOfWeek != DayOfWeek.Saturday && dt2.DayOfWeek != DayOfWeek.Sunday)
                {
                    dayCount++;
                }
               
            }

Open in new window

0
devo00Author Commented:
Yeah I definitely need hours, as in, if someone starts a ticket on Friday and happens to close it on a Saturday, none of the hours from Saturday count, and only the partial day (in hours) for Friday will count.
I'm not seeing a straight-forward way to do this above, would we convert the day count to a decimal and multiply by 24 at the end?
0
nullcoryCommented:
I didn't have a chance to fully debug this but it looks like it works.
DateTime dt2 = DateTime.Now;
 
            DateTime dt = DateTime.Parse("10/15/2008");
 
            TimeSpan dt3 = dt2 - dt;
 
            int hourCount = 0;
            int totalHours = 0;
 
            totalHours += dt3.Days * 24;
            totalHours += dt3.Hours;
 
            //1 hour timespan
            TimeSpan hour = new TimeSpan(1, 0, 0);
 
            for (int i = 1; i < totalHours+1; i++)
            {
                dt2 = dt2.Subtract(hour);
 
                if (dt2.DayOfWeek != DayOfWeek.Saturday && dt2.DayOfWeek != DayOfWeek.Sunday)
                {
                    hourCount++;
                }
            }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
devo00Author Commented:
Excellent, thank you!  It seems to work for every test I've done so far.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.