Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Calculating Work Week Ranges

Posted on 2006-07-12
6
Medium Priority
?
952 Views
Last Modified: 2008-02-01
I have two things I wish to accomplish:

1)  Based on the current date determine the StartDate of the current Work Week  and the End Date of the current Work Week.  

2)  Based on the current date generate a list of  the prior 10 work week ranges and the upcoming 10 work week ranges.  

 I would like to implement both of these as public methods within a class.  My language of choice is C#

Thanks In Advance
0
Comment
Question by:DylanJones1
[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
6 Comments
 
LVL 35

Expert Comment

by:mrichmon
ID: 17092335
It is simply math.

Assume that a week starts on Sunday and ends on SAtruday.  Then the formula is:

DateTime today = DateTime.Now.Date;
DateTime weekStart;
DateTime weekEnd;

// Calculate the start date
weekStart = today.AddDays(-((int)today.DayOfWeek));

// End date can be calculated based on adding 6 days to weekStart or manually as:
weekEnd = today.AddDays(6-((int)today.DayOfWeek));

As for the 10 work ranges, just do a loop subtracting 7 days from teh start date for each week range you want.
0
 
LVL 1

Expert Comment

by:flashaoy
ID: 17092336
There is one big problem with work week calculations. Observered holidays, see :http://www.codeproject.com/dotnet/HolidayCalculator.asp for that. After that work week is trivial.

0
 
LVL 7

Expert Comment

by:whatsit2002
ID: 17092534
mrichmon seems to have the correct solution so please do not award any points to this comment. I just wanted to chime in and and say, this is how you do it in SQL just in case someone stumbles across this and wonders:

DECLARE @Today AS DATETIME
SET @Today = GETDATE()

SELECT DATEADD(wk, DATEDIFF(wk, 0, @Today), 0) AS Monday, DATEADD(wk, DATEDIFF(wk, 4, @Today), 4) AS Friday


Jason
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:DylanJones1
ID: 17092659
Want to thank you all for you quick input.  I guess the wrinkle I failed to communicate is that I need to determine the current range on any given day.


Given today  07/12/2006 the current date range is

Work week start date 07/10/2006   work week end date  07/15/2006 - we work saturdays :-(

So basically given the current date I need to determine the date of the prior monday and the date of the upcoming saturday.

0
 
LVL 35

Expert Comment

by:mrichmon
ID: 17092662
flashaoy,

It actually depends.  At least in all of the applications I have written or worked on a work week is a work week, regardless of holidays.  Now business days are a different matter.  A work week may or may not consist of 5 business days depending on holidays.

So my above is for a work week - not business days.
0
 
LVL 35

Accepted Solution

by:
mrichmon earned 2000 total points
ID: 17092676
Very easy.  My solution is for Suday to Satruday.

If you want Monday to Satureday then simply do:

weekStart = today.AddDays(-((int)today.DayOfWeek)).AddDays(1);
weekEnd = today.AddDays(6-((int)today.DayOfWeek));
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

604 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