计算员工有效工作时间(C#版)

  /// <summary>
        /// 更具开始时间和结束时间获得有效的工作时间
        /// </summary>
        /// <param name="startime"></param>
        /// <param name="endtime"></param>
        /// <returns></returns>
        public static double GetWorkTime(string startime, string endtime)
        {
            if (startime == endtime)
            {
                return 0;
            }
            //计算结束时间和开始时间之间的时间差
            DateTime start = Convert.ToDateTime(startime);
            //结束时间
            DateTime end = string.IsNullOrEmpty(endtime) ? DateTime.Now : Convert.ToDateTime(endtime);
            //设置开始时间的标准工作时间
            DateTime startWorkSTime = Convert.ToDateTime(start.ToString("yyyy-MM-dd") + " " + "09:00:00");
            DateTime startWorkETime = Convert.ToDateTime(start.ToString("yyyy-MM-dd") + " " + "17:00:00");
            //设置结束时间的标准工作时间
            DateTime endWorkSTime = Convert.ToDateTime(end.ToString("yyyy-MM-dd") + " " + "09:00:00");
            DateTime endWorkETime = Convert.ToDateTime(end.ToString("yyyy-MM-dd") + " " + "17:00:00");

            TimeSpan ts = end - start;
            int day = ts.Days;
            double minute = 0.0;

            if (day > 0)
            {
                //当结束时间点钟比开始时间大天数减一
                if (DateTime.Compare(Convert.ToDateTime(start.ToString("HH:mm:ss")), Convert.ToDateTime(end.ToString("HH:mm:ss").ToString())) <= 0)
                {
                    day = day - 1;
                }
                DateTime xunhuan = start;
                minute = day * 8 * 60;
                for (int i = 0; i < day; i++)
                {
                    if (xunhuan.AddDays(1).DayOfWeek.ToString() == "Saturday" || xunhuan.AddDays(1).DayOfWeek.ToString() == "Sunday")
                    {
                        minute = minute - 8 * 60;
                    }
                    xunhuan = xunhuan.AddDays(1);
                }

            }
            //计算开始时间
            if (start.DayOfWeek.ToString() != "Saturday" && start.DayOfWeek.ToString() != "Sunday")
            {
                //计算开始时间落在九点之前
                if (DateTime.Compare(start, startWorkSTime) <= 0)
                {
                    minute += 8 * 60;
                }
                //计算时间落在上午九点和下午5点之间
                else if (DateTime.Compare(startWorkSTime, start) < 0 && DateTime.Compare(start, startWorkETime) <= 0)
                {
                    minute += (startWorkETime - start).TotalMinutes;
                }
            }
            //循环计算排除双休日

            if (end.DayOfWeek.ToString() != "Saturday" && end.DayOfWeek.ToString() != "Sunday")
            {
                //结束时间在九点和下午5点之间
                if (DateTime.Compare(endWorkSTime, end) < 0 && DateTime.Compare(end, endWorkETime) <= 0)
                {
                    minute += (end - endWorkSTime).TotalMinutes;
                }
                //结束时间在5点以后
                else if (DateTime.Compare(endWorkETime, end) <= 0)
                {
                    minute += 8 * 60;
                }
            }
            if (day == 0)
            {
                //不在同一天跨度时间不一致
                //|| ((start.ToString("yyyy-MM-dd") == end.ToString("yyyy-MM-dd")) && (DateTime.Compare(startWorkSTime, start) <= 0&& DateTime.Compare(start, startWorkETime) <= 0 && DateTime.Compare(start, end) <= 0&& DateTime.Compare(end, startWorkETime) < 0)))
                if (!(((start.ToString("yyyy-MM-dd") != end.ToString("yyyy-MM-dd")) && ((DateTime.Compare(startWorkSTime, start) <= 0 && DateTime.Compare(start, startWorkETime) <= 0) && (DateTime.Compare(endWorkSTime, end) <= 0 && DateTime.Compare(end, endWorkETime) <= 0)))))
                {
                    minute = (minute - 8 * 60) > 0 ? (minute - 8 * 60) : 0;
                }
            }
            return Math.Round(minute / 60, 1);
        }
    原文作者:东边的小山
    原文地址: https://blog.csdn.net/yelin042/article/details/78581088
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞