Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-10-27
7
Medium Priority
?
1,461 Views
Last Modified: 2013-11-07
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?
0
Comment
Question by:devo00
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 12

Expert Comment

by:jazzIIIlove
ID: 22816701
Hi there;

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

Best regards...
0
 
LVL 12

Expert Comment

by:jazzIIIlove
ID: 22816706
or algorithm?
0
 

Author Comment

by:devo00
ID: 22816761
Sure, so far I only display the flat difference in hours (see snippet). I need to adapt it to exclude weekends.

snippet.txt
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 3

Expert Comment

by:nullcory
ID: 22817887
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
 

Author Comment

by:devo00
ID: 22821384
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
 
LVL 3

Accepted Solution

by:
nullcory earned 2000 total points
ID: 22824034
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
 

Author Closing Comment

by:devo00
ID: 31510525
Excellent, thank you!  It seems to work for every test I've done so far.
0

Featured Post

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question