473,473 Members | 2,185 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

TimeSpan between Business Days

15 New Member
Hi,
I was looking for the code to return Timespan between any two days. The days should exclude weekends, saturday and sunday.
Like, for example,
startdate = "10/5/2007 10:00:00 AM"
enddate = "10/9/2007 11:30:00 AM"

The function should return the Timespan between these two days excluding Saturday and Sunday...
Timespan ts = 1:12:30:00

Thanks in Advance,
PKJ
Oct 7 '07 #1
6 4918
Shashi Sadasivan
1,435 Recognized Expert Top Contributor
Hi,
I was looking for the code to return Timespan between any two days. The days should exclude weekends, saturday and sunday.
Like, for example,
startdate = "10/5/2007 10:00:00 AM"
enddate = "10/9/2007 11:30:00 AM"

The function should return the Timespan between these two days excluding Saturday and Sunday...
Timespan ts = 1:12:30:00

Thanks in Advance,
PKJ
Expand|Select|Wrap|Line Numbers
  1. DateTime startDate = DateTime.Now;
  2.             DateTime endDate = DateTime.Now.AddDays(20);
  3.             TimeSpan ts = endDate.Subtract(startDate);
  4.             Console.WriteLine(ts.ToString());
  5.             int weekends = 0;
  6.             for (DateTime tempDt = startDate; tempDt < endDate; tempDt = tempDt.AddDays(1))
  7.             {
  8.                 if (tempDt.DayOfWeek == DayOfWeek.Saturday || tempDt.DayOfWeek == DayOfWeek.Sunday)
  9.                     weekends++;
  10.             }
  11.             ts = ts.Subtract(new TimeSpan(weekends, 0, 0, 0));
  12.             Console.WriteLine(ts.ToString());
does this help you?

cheers
Oct 7 '07 #2
pkj7461
15 New Member
Hi,
Sorry for delay in replying.
This seems to be not working. It gives the timespan including weekends.
Thanks,
Prasanna






Expand|Select|Wrap|Line Numbers
  1. DateTime startDate = DateTime.Now;
  2.             DateTime endDate = DateTime.Now.AddDays(20);
  3.             TimeSpan ts = endDate.Subtract(startDate);
  4.             Console.WriteLine(ts.ToString());
  5.             int weekends = 0;
  6.             for (DateTime tempDt = startDate; tempDt < endDate; tempDt = tempDt.AddDays(1))
  7.             {
  8.                 if (tempDt.DayOfWeek == DayOfWeek.Saturday || tempDt.DayOfWeek == DayOfWeek.Sunday)
  9.                     weekends++;
  10.             }
  11.             ts = ts.Subtract(new TimeSpan(weekends, 0, 0, 0));
  12.             Console.WriteLine(ts.ToString());
does this help you?

cheers
Oct 7 '07 #3
Shashi Sadasivan
1,435 Recognized Expert Top Contributor
Could you please give some input dates (start and end).

I tested it, and it was working allright.
Once you send some test data.....I can check it again.

Have you in anyways modified the code anywhere?

cheers
Oct 7 '07 #4
pkj7461
15 New Member
Hi Shashi,
I was working on weekend with the code. I changed the input to
Start Date = "10/4/2007 3:26:00 PM"
endDate = " 10/7/2007 9:24:00 PM"

I ran the code and changed the format of output to "hh:mm"
My output was 29:58
and the actual output should be 32:33.
I noticed one more thing. The output seems to be changing every minute even during weekend.
Thank you very much for your time and efforts.
Prasanna.



Could you please give some input dates (start and end).

I tested it, and it was working allright.
Once you send some test data.....I can check it again.

Have you in anyways modified the code anywhere?

cheers
Oct 8 '07 #5
Shashi Sadasivan
1,435 Recognized Expert Top Contributor
Checking your specifications :
you wanted the timespan between business days :)
But in your first post you wanted between any 2 days.

I went along with the title "between 2 business days"
(why do you want staff to work on weekends :P)
7th of 10 is not a business day.

so now you know where to modify the code.

cheers
Oct 8 '07 #6
Plater
7,872 Recognized Expert Expert
Well DateTime objects have that "Day of the week" property.
You can create DateTime objects for each whole "day" inbetween your two dates. If the dayoftheweek is not a weekend day, add 24?
That's a really simplified reasoning on it, but then you just have to find the "used" time on the start/end day, assuming they are "week days"
Oct 8 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jeff Shantz | last post by:
Hello, I'm developing a large statistics application for a call center. It often needs to calculate time spanning over months. For example, an agent's total talk time within 30 days. Since an...
6
by: charliewest | last post by:
Can someone pls point me to or recommend the easiest way to calculate someone´s age using the TimeSpan object, in .NET CF? Isn´t there a simple way to use the TimeSpan object to calculate the...
2
by: Dennis D. | last post by:
Hello: I want to subtract a timespan from the selectionrange.start of a month calendar. The selectionrange.start of a month calendar is a date, and it is possible to subtract a timespan from a...
13
by: sd00 | last post by:
Hi all, can someone give me some coding help with a problem that *should* be really simple, yet I'm struggling with. I need the difference between 2 times (Target / Actual) However, these times...
5
by: style | last post by:
Hello In my custom configuration section, I'd like to specify an interval attribute which specifies an interval in milliseconds. I think it would be very comfortable to get this value directly...
4
by: Peter Proost | last post by:
Hi group, it's been a long time since the last time I've been here but I have a question. I'm working with timespan.parse for calculating a duration, I have to add strings which are in the...
5
by: prasanta.bhowmik | last post by:
Hello, I created a object of TimeSpan only with minute and expected the result in hours and minute, but its returning wrong result. My Code is : TimeSpan timeSpan = new TimeSpan(0,2400,0);...
4
by: kellygreer1 | last post by:
I haven't worked with the TimeSpan object before. So bare with me if this seems like a newb question. But if I wanted to know how many days from the current date until 2/4/2008. I have written...
2
by: shapper | last post by:
Hello, I have the following: boxStat = new BoxStat { BoxCount = database.Boxes.Count(), SinceLastCreate = (DateTime.Now - database.Boxes.Max(b => b.CreatedAt)).Days ?? 0 };
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.