Working with shift schedules and dates

Posted on 2009-02-19
Last Modified: 2013-11-16
I'm trying to determine the best way to tackle a calendar view of 4 shifts.  These shifts are designed as:

Day Shift A =   2on, 2off, 3on then 2off, 2on, 3off then repeat.
Night Shift B =  2on, 2off, 3on then 2off, 2on, 3off then repeat.
Day Shift C =    2off, 2on, 3off then 2on, 2off, 3on then repeat.
Night Shift D =  2off, 2on, 3off then 2on, 2off, 3on then repeat.

I've used a method long ago that determined opposite shifts by using the mod function, but this one has me scratching my head.  I have 2 goals here.  

One is having a calendar that can plot these shifts.  I'll probably use the .net calendar for this.  

The second is allowing the user to choose a date and then providing them the folks that will be working on that day.  Or more likely allowing them to navigate the calendar and it give them the viewable schedule as the attached image shows.

Thank you for your time,
Question by:westbergk
    LVL 24

    Accepted Solution

    Two things you need. The first is the starting date of the cycle.

    After that, you're dealing with a 14-day cycle. The fact that shifts run opposite each other simplifies life.

    Find the difference (in days) between the date of interest and the start date. Then run mod 14 to get the position in the cycle.

    Days 1, 2, 5, 6, 7, 10 and 11 are shift A/B on; other days are shift C/D.

        Public Function GetShift(ByVal startDate As DateTime, ByVal currentDate As DateTime) As String
            Dim days As Integer = DateDiff(DateInterval.Day, startDate, currentDate) + 1
            Dim cyclePosition As Integer = days Mod 14
            Select Case cyclePosition
                Case 1, 2, 5, 6, 7, 10, 11
                    Return "A/B"
                Case Else
                    Return "C/D"
            End Select
        End Function

    Open in new window

    LVL 1

    Author Comment

    Thank you Chaosian, that was fast... I must be a real doof.

    So the starting date should be one that places these shifts in the correct position?  Shouldnt matter what date, as long as it is in the past?  Makes sense.

    So if i were to use the .net calendar, do you think I should simply run the date against this function during the OnDayRender and then do some formating magic per each visible day of the calendar?

    LVL 24

    Expert Comment

    by:Jeff Certain
    Well, I've dealt with date problems a time or twelve :)

    Your OnDayRender idea is going to be the easy way of doing things. Since it's a pretty fast algorithm, you shouldn't see a lot of perf issues, unless you're trying to view a ton of dates at the same time.

    If you wanted to show a couple years' worth of data at once, you'd likely need to do some fancy work around performance, but it's not worth it otherwise.
    LVL 24

    Expert Comment

    by:Jeff Certain
    And, yes... any date in the past that is the first day team A/B comes on rotation should work as the startDate.
    LVL 1

    Author Closing Comment

    Awesome.  I'll give it a go.  Thank you Chaosian.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now