troubleshooting Question

Calculate days between dates excluding weekends

Avatar of tip2tail
tip2tailFlag for United Kingdom of Great Britain and Northern Ireland asked on
C#
8 Comments1 Solution1753 ViewsLast Modified:
Hello all,

I am developing an application that will keep a record of "Parts" that are brought into the department and then charged out again to other departments.  The application will be able to monitor the time critical status of parts that must be used within 5 days to avoid a further charge.

I need to be able to get the number of days between the current date and the "inDate" of the part.  I then format my grid as required as can be seen in the code snippet.  The criteria is that 0 - 1 days - OK - Green; 2 - 4 days - WARNING - Yellow; 5+ days - RED - part handling charge applied.  This seems like a fairly simple problem with the exception of weekends which do not count.  So a part in on Friday should not be Yellow until Tuesday and not Red until Thursday onwards - ignoring Sat and Sun.

The other issue is that the department is open on a Saturday and the application must be able to take this into account - that the Today's Date might be a non working day and still calculate correctly.

I require some help in desigining a getDateDiff() function (returning an int) toi be used in the manner shown in the code snippet.

Please help!

Thanks,

Mark Young
DateTime inDate, toDate;
                    inDate = Convert.ToDateTime(gridParts.Rows[i].Cells["pDateInDataGridViewTextBoxColumn"].Value.ToString());
                    inDate = new DateTime(inDate.Year, inDate.Month, inDate.Day);
                    toDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
 
                    if (getDateDiff(inDate, toDate) < 2)
                    {
                        MessageBox.Show("G:" + getDateDiff(inDate, toDate));
                        gridParts.Rows[i].Cells["colIcon"].Value = Properties.Resources.emoticon_happy;
                    }
                    else if ((getDateDiff(inDate, toDate) >= 2) && (getDateDiff(inDate, toDate)) < 4)
                    {
                        MessageBox.Show("Y:" + getDateDiff(inDate, toDate));
                        gridParts.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
                        gridParts.Rows[i].Cells["colIcon"].Value = Properties.Resources.error;
                        yellowTotal = yellowTotal + 1;
                    }
                    else
                    {
                        MessageBox.Show("R:" + getDateDiff(inDate, toDate));
                        gridParts.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                        gridParts.Rows[i].Cells["colIcon"].Value = Properties.Resources.emoticon_unhappy;
                        redTotal = redTotal + 1;
                    }
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 8 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros