Wednesday, December 13, 2006

Calculating working days between 2 dates

Here is a function to calculate the number of working days given two dates. It assume the working days is from Monday to Friday, both dates are inclusive and no holidays in between.



static int CalculateWorkingDays(DateTime start, DateTime end)
{
DateTime firstSunday, lastSaturday;
int workDays = 0;

if (start.DayOfWeek != DayOfWeek.Sunday)
{
firstSunday = start.AddDays(7 - ((int)start.DayOfWeek));
workDays += (6 - (int)start.DayOfWeek);
}
else
firstSunday = start;


if (end.DayOfWeek != DayOfWeek.Saturday)
{
lastSaturday = end.AddDays(-((int)end.DayOfWeek) - 1);
workDays += (int)end.DayOfWeek;
}
else
lastSaturday = end;

TimeSpan ts = lastSaturday.Subtract(firstSunday);
workDays += ((ts.Days + 1) / 7) * 5;

return workDays;
}

Labels:

Friday, December 08, 2006

VS2005 : Reference .NET 3.0 Assemblies

I installed .NET Framework 3.0 just a while ago and I realize that the new assemblies in.NET 3.0 does not show in the Reference dialog box when you Add Reference in your VS 2005project.
To enable this, you have to manually add a key into the registry. By default, when you install.NET 3.0, the assemblies is installed into the folder:

C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0

To enable the 3.0 assemblies to show in the Reference dialog, create a .reg file and put the following into the file. Then double click the .reg file to merge it into the registry.
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\8.0\AssemblyFoldersFramework 3.0]
@="C:\\Program Files\\Reference Assemblies\\Microsoft\\Framework\\v3.0"


Now restart yout Visual Studio 2005, create a new project or open any existing project,then open Add Reference dialog box in the project, you should now see the new 3.0 assemblies.

VS 2005 Add Reference for .NET 3.0 assemblies.

Labels: