C# Source Code: Simple function to Add a number of weekdays to a date
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
C# Source Code
Simple function to Add a number of weekdays to a date
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Tuesday, January 29, 2008
Hits:
3042
Category:
General/Framework
Article:
Below is a simple function to add or subtract a number of weekdays to a specified date. ///
/// Takes a reference date and add or subtracts a specified number of weekdays. ///
///
The reference date. ///
The number of days to offset by. ///
The reference date plus or minus the specified number of weekdays.
private DateTime AddWeekDays( DateTime date, int offset) { int daysAdded = 0; int addDay = 1; if (offset < 0) { addDay = -1; } DateTime result = date; do { if (daysAdded == offset) { break; } if (result.DayOfWeek != DayOfWeek.Saturday && result.DayOfWeek != DayOfWeek.Sunday) { daysAdded = daysAdded + addDay; } result = result.AddDays(addDay); } while (true); return result; }
Terms and Conditions
Support this site
Download a trial version of the best FTP application on the internet