首页 > 解决方案 > 使用 c# 计算开始和停止时钟的总计数

问题描述

在我的应用程序中,我有一个用于发送剩余部分的开始和停止时钟。每当时钟开始和停止时,它将识别开始和停止日期,并将消除所有公共假期和周末。我成功地消除了公共假期和周末。现在我的问题是我需要遍历所有工作日,并且应该确定时钟是从上午还是下午开始,以确定时钟是覆盖一整天还是半天。有一个 Count 变量,初始化为 0.5,每半天后递增(每个 AM 和 PM 之后)。不知道如何在工作日的每半天循环并找到总数。

删除了所有周末和节假日。

public double ElapseDays(string caseNo, string DatePart)
{
    double count = 0;
    double weekEndsCount;
    Boolean excludeWeekends = true;

    using (var db = new EMSDEntities())
    {
        var q = (from a in db.SY_HOLIDAYS
                 orderby a.HLD_DATE
                 select new HolidayListViewModel
                 {
                    HolidayDate = a.HLD_DATE,

                 }).ToList();

        for (DateTime index = '01/07/2019'; index <= '03/07/2019'; index = index.AddDays(1))
        {
            bool excluded = false;
            if (excludeWeekends && index.DayOfWeek != DayOfWeek.Sunday && index.DayOfWeek != DayOfWeek.Saturday)
            {               
                for (int i = 0; i < q.Count; i++)
                {
                    if (index.Date.CompareTo(q[i].HolidayDate) == 0)
                    {
                        excluded = true;
                        break;
                    }
                } 
            }
            if (!excluded)
            {
                count = count + 0.5;
            }
        }
    }
}

预期产出

<!DOCTYPE html>
<html>
<body>

<h2>Basic HTML Table</h2>

<table style="width:100%">
  <tr>
    <th>start_date</th>
    <th>end_date</th> 
  </tr>
  <tr>
    <td>01/07/2019 PM</td>
    <td>03/07/2019 PM</td>
  </tr>

</table>

</body>
</html>

计数应为 2.5,因为时钟开始于 (01/07/2019) 下午(半天)。因此它将初始化计数为 0.5 并在 (03/07/2019)PM(full day) 停止

标签: c#

解决方案


推荐阅读