C#跳过工作日,计算几个工作日之后到期的方法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#跳过工作日,计算几个工作日之后到期的方法相关的知识,希望对你有一定的参考价值。

需求:消费者投诉企业,企业在2个工作日之内做出应答。

 

分析:1.工作日要刨去周末和法定节假日,而且每年的节假日不一样。

2.消费者可以在任意时间发起投诉,如果在非工作日发起了投诉,那么计算时间应该从工作日开始计算,就要刨去当日的时和分。

 

以下是代码:

技术分享
public class HolidayHelper

    {

        /// <summary>

        /// 判断法定工作日

        /// </summary>

        /// <param name="dt">时间</param>

        /// <returns>0上班,1不需要上班的周末(也包括节假日),2法定节假日</returns>

        public static int IsHolidayOrWeek(DateTime dt)

        {

            //特殊的周末(周末却上班)

            string[] specalWeek2009 = { "0104", "0124", "0201", "0531", "0927", "1010" };

            string[] specalWeek2010 = { "0220", "0221", "0612", "0613", "0919", "0925", "0926", "1009" };

            string[] specalWeek2011 = { "0130", "0212", "0402", "1008", "1009" };

            string[] specalWeek2012 = { "0129", "0331", "0401", "0428", "0929" };

            string[] specalWeek2013 = { "0105", "0106", "0216", "0217", "0407", "0427", "0428", "0608", "0609", "0922", "0929", "1012" };

            string[] specalWeek2014 = { "0126", "0208", "0504", "0928", "1011" };

            string[] specalWeek2015 = { "0104", "0215", "0228", "0906", "1010" };

            string[] specalWeek2016 = { "0206", "0214", "0612", "0918", "1008", "1009" };

            string[] specalWeek2017 = { "0122", "0204", "0401", "0527", "0930" };

 

            //法定假日

            string[] holiday2009 = { "0101", "0102", "0103", "0125", "0126", "0127", "0128", "0129", "0130", "0131", "0404", "0405", "0406", "0501", "0502", "0503", "0528", "0529", "0530", "1001", "1002", "1003", "1004", "1005", "1006", "1007", "1008", };

            string[] holiday2010 = { "0101", "0102", "0103", "0213", "0214", "0215", "0216", "0217", "0218", "0219", "0403", "0404", "0405", "0501", "0502", "0503", "0614", "0615", "0616", "0922", "0923", "0924", "1001", "1002", "1003", "1004", "1005", "1006", "1007" };

            string[] holiday2011 = { "0101", "0102", "0103", "0202", "0203", "0204", "0205", "0206", "0207", "0208", "0403", "0404", "0405", "0430", "0501", "0502", "0604", "0605", "0606", "0910", "0911", "0912", "1001", "1002", "1003", "1004", "1005", "1006", "1007", };

            string[] holiday2012 = { "0101", "0102", "0103", "0122", "0123", "0124", "0125", "0126", "0127", "0128", "0402", "0403", "0404", "0429", "0430", "0501", "0622", "0623", "0624", "0930", "1001", "1002", "1003", "1004", "1005", "1006", "1007", };

            string[] holiday2013 = { "0101", "0102", "0103", "0209", "0210", "0211", "0212", "0213", "0214", "0215", "0404", "0405", "0406", "0429", "0430", "0501", "0610", "0611", "0612", "0919", "0920", "0921", "1001", "1002", "1003", "1004", "1005", "1006", "1007", };

            string[] holiday2014 = { "0101", "0131", "0201", "0202", "0203", "0204", "0205", "0206", "0405", "0406", "0407", "0501", "0502", "0503", "0602", "0908", "1001", "1002", "1003", "1004", "1005", "1006", "1007", };

            string[] holiday2015 = { "0101", "0102", "0103", "0218", "0219", "0220", "0221", "0222", "0223", "0224", "0404", "0405", "0406", "0501", "0502", "0503", "0620", "0621", "0622", "0903", "0904", "0905", "0927", "1001", "1002", "1003", "1004", "1005", "1006", "1007", };

            string[] holiday2016 = { "0101", "0207", "0208", "0209", "0210", "0211", "0212", "0213", "0404", "0501", "0502", "0609", "0610", "0611", "0915", "0916", "0917", "1001", "1002", "1003", "1004", "1005", "1006", "1007", };

            string[] holiday2017 = { "0101", "0102", "0127", "0128", "0129", "0130", "0131", "0201", "0202", "0403", "0404", "0501", "0529", "0530", "1001", "1002", "1003", "1004", "1005", "1006", };

 

            bool isHoildayOrWeek = false;

            //取年

            string weekYear = dt.Year.ToString();

            //取月日

            string[] weekDate = { dt.ToString("MMdd") };

            //判断周末

            if ((int)dt.DayOfWeek == 0 || (int)dt.DayOfWeek == 6)

            {

                //周末是否需要上班

                switch (weekYear)

                {

                    case "2009":

                        isHoildayOrWeek = specalWeek2009.Intersect(weekDate).Count() == 0;

                        break;

                    case "2010":

                        isHoildayOrWeek = specalWeek2010.Intersect(weekDate).Count() == 0;

                        break;

                    case "2011":

                        isHoildayOrWeek = specalWeek2011.Intersect(weekDate).Count() == 0;

                        break;

                    case "2012":

                        isHoildayOrWeek = specalWeek2012.Intersect(weekDate).Count() == 0;

                        break;

                    case "2013":

                        isHoildayOrWeek = specalWeek2013.Intersect(weekDate).Count() == 0;

                        break;

                    case "2014":

                        isHoildayOrWeek = specalWeek2014.Intersect(weekDate).Count() == 0;

                        break;

                    case "2015":

                        isHoildayOrWeek = specalWeek2015.Intersect(weekDate).Count() == 0;

                        break;

                    case "2016":

                        isHoildayOrWeek = specalWeek2016.Intersect(weekDate).Count() == 0;

                        break;

                    case "2017":

                        isHoildayOrWeek = specalWeek2017.Intersect(weekDate).Count() == 0;

                        break;

                }

            }

            if (isHoildayOrWeek)

            {

                return 1;

            }

            //判断法定节假日

            switch (weekYear)

            {

                case "2009":

                    isHoildayOrWeek = holiday2009.Intersect(weekDate).Count() > 0;

                    break;

                case "2010":

                    isHoildayOrWeek = holiday2010.Intersect(weekDate).Count() > 0;

                    break;

                case "2011":

                    isHoildayOrWeek = holiday2011.Intersect(weekDate).Count() > 0;

                    break;

                case "2012":

                    isHoildayOrWeek = holiday2012.Intersect(weekDate).Count() > 0;

                    break;

                case "2013":

                    isHoildayOrWeek = holiday2013.Intersect(weekDate).Count() > 0;

                    break;

                case "2014":

                    isHoildayOrWeek = holiday2014.Intersect(weekDate).Count() > 0;

                    break;

                case "2015":

                    isHoildayOrWeek = holiday2015.Intersect(weekDate).Count() > 0;

                    break;

                case "2016":

                    isHoildayOrWeek = holiday2016.Intersect(weekDate).Count() > 0;

                    break;

                case "2017":

                    isHoildayOrWeek = holiday2017.Intersect(weekDate).Count() > 0;

                    break;

            }

            if (isHoildayOrWeek)

            {

                return 2;

            }

            return 0;

 

 

        }

 

 

        /// <summary>

        /// 工作日时间差

        /// </summary>

        /// <param name="days">几个工作日后</param>

        /// <returns>延迟毫秒数</returns>

        public static double GetDelpyTime(int days)

        {

            int sign = 0;

            for (int i = 0; i < days; i++)

            {

                if (i == 0 && IsHolidayOrWeek(DateTime.Now.AddDays(i)) > 0)

                {

                    //非工作日发起的投诉,要抹掉小时和分钟                   

                    sign = 1;

                }

                if (IsHolidayOrWeek(DateTime.Now.AddDays(i)) > 0)

                {

                    days = days + 1;

                };

            }

            TimeSpan ts = new TimeSpan();

            if (sign == 1)

            {

                ts = (DateTime.Now.AddDays(days) - DateTime.Now.AddHours(DateTime.Now.Hour).AddMinutes(DateTime.Now.Minute));

            }

            else

            {

                ts = DateTime.Now.AddDays(days) - DateTime.Now;

            }

            return ts.TotalMilliseconds;

        }

    }
View Code

 

谢谢大家。

 

打赏随意:

技术分享

以上是关于C#跳过工作日,计算几个工作日之后到期的方法的主要内容,如果未能解决你的问题,请参考以下文章

一个简单的时间片轮转多道程序内核操作系统工作流程

C#如何计算不包括特定日期(节假日)的工作日

linux内核分析作业:操作系统是如何工作的进行:完成一个简单的时间片轮转多道程序内核代码

使用 C# 读取 Excel 工作表,其中表格已合并单元格

第二周:一个简单的时间片轮转多道程序内核代码及分析

图像处理中几个基本的处理方法c#代码实现