473,587 Members | 2,494 Online
Bytes | Software Development & Data Engineering Community
+ 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 4946
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
21041
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 agent talks about 5 hours per day, this comes out to about 150 hours of talk time per month. Fine. But, the DateTime and TimeSpan format strings will only output hours from 0-23. I need something that will output time as such: 150:0:0 to...
6
31789
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 elapsed time and somehow convert the days to years w/out using the VB DLL? Thanks.
2
1822
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 date and get a date, from which I will extract the day. How do I assign an integer to represent an 'n' day timespan, especially without using ticks?
13
2132
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 will fall somewhere between a Start & End time Further more, there will be Break & Lunch times between Start & End. Example... Start 08:00 Break start 10:30
5
9780
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 as a TimeSpan. That's what I tryied: public TimeSpan Interval {
4
3283
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 following format 15:36:12 ==Hours, minutes, seconds but sometimes there is a string like 55:26:32 ==which is correct cause the production machine was busy for 55 hours, but the timespan.parse crashes on this value because it expects 2.7:26:32. I've...
5
5505
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); Console.WriteLine(timeSpan.Hours+':'+timeSpan.Minutes); Its Displaying me the result is 16:00, but expected result is 40:00.
4
2772
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 this much code so far. DateTime startDate = new DateTime("2008-02-04"); DateTime curDate = DateTime.Now(); TimeSpan ts = startDate.Subtract(curDate); Now I get lost. I was expecting to find a Days property off of the
2
12609
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
7924
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7854
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8219
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
7978
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6629
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5722
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2364
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.