Skip to main content

Business Days in c# (a very fast way)

Hi,

Today I needed a simple code that returns the business days between two dates and ended with this:

public static int GetFullWorkingDaysBetween(DateTime firstDate, DateTime lastDate, IEnumerable holidays)
        {
            if (firstDate > lastDate)// Swap the dates if firstDate > lastDate
            {
                DateTime tempDate = firstDate;
                firstDate = lastDate;
                lastDate = tempDate;
            }
            int days = (int)(lastDate.Subtract(firstDate).Ticks / TimeSpan.TicksPerDay);
            int weekReminder = days % 7;
            if (weekReminder > 0)
            {
                switch (firstDate.DayOfWeek)
                {
                    case DayOfWeek.Monday:
                        days = days - ((weekReminder > 5) ? 1 : 0);
                        // Another way for this:
                        //days = days - ((int)weekReminder % 5);
                        // but i think its more expensive
                        break;
                    case DayOfWeek.Tuesday:
                        days = days - ((weekReminder > 4) ? 1 : 0) - ((weekReminder > 5) ? 1 : 0);
                        // The same from above
                        //days = days - ((int)weekReminder % 4);
                        break;
                    case DayOfWeek.Wednesday:
                        days = days - ((weekReminder > 3) ? 1 : 0) - ((weekReminder > 4) ? 1 : 0);
                        break;
                    case DayOfWeek.Thursday:
                        days = days - ((weekReminder > 2) ? 1 : 0) - ((weekReminder > 3) ? 1 : 0);
                        break;
                    case DayOfWeek.Friday:
                        days = days - ((weekReminder > 1) ? 1 : 0) - ((weekReminder > 2) ? 1 : 0);
                        break;
                    case DayOfWeek.Saturday:
                        days = days - 1 - ((weekReminder > 1) ? 1 : 0);
                        break;
                    case DayOfWeek.Sunday:
                        days = days - 1;
                        break;
                }
            }
            days = days - (2 * ((int)days / 7));
            if (holidays != null && holidays.Count() > 0)
            {
                foreach (DateTime holiday in holidays.Where(h => h >= firstDate && h < lastDate))
                {
                    DayOfWeek dayOfWeekOfHoliday = holiday.DayOfWeek;
                    if (dayOfWeekOfHoliday != DayOfWeek.Saturday && dayOfWeekOfHoliday != DayOfWeek.Sunday)
                    {
                        days = days - 1;
                    }
                }
            }
            return days;
        }

Comments

Popular posts from this blog

Wpf, TreeView and Mvvm: Conceptual Findings (Part 1)

I have read a lot of articles, comments and how To's (see below for a brief list of links) about the use of MVVM with TreeView (WPF & C#), but none of them convince me for the proof of concept that i have in mind. Why? Because i think that the Drag & Drop operation is, only, a View Operation. As i understand the MVVM is like:  Model: Data and data extract/insert operations ViewModel: Commands, Main Logic and so on... View: User Experience After defining the concepts, i want to note something: the view can be a console application, a web page, a window application or a touch application. I'm not sure that if the view is a console app, there must be commands to handle the drag and drop operations like Sparky at al mentioned (we can have an interface defining the ViewModel and a lot of pairs classes (View & ViewModel), but then, where is the re usability of the code?) Let me be clear about something, the idea of Sparky is very interesting (and clear) so it was

Use fastJSON with WCF (C#)

To use fastJson Serializer with WCF Web Service: Within a Web Site or App Project (VS), Add AjaxService Add: Method: Get, Use: Only to retrieve Data [ OperationContract ] [ WebGet (ResponseFormat = WebMessageFormat .Json)] public Stream TestStream( string ticker) { WebOperationContext .Current.OutgoingResponse.ContentType = "application/json" ; string jsonString= "{\"d\":{}}" ;//Here you do the serialization MemoryStream ms = new MemoryStream ( ASCIIEncoding .Default.GetBytes(jsonString)); return ms; } Method: Post, Use: Insert data / data manipulation [ OperationContract ] [ WebInvoke (Method= "POST" , ResponseFormat = WebMessageFormat .Json)] public Stream TestStream( string ticker) { WebOperationContext .Current.OutgoingResponse.ContentType = "application/json" ; string jsonString= "{\"d\":{}}" ;//Here you do the serialization MemoryStream ms = new MemoryStream ( ASCIIEncoding .Def