加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 获取时间段之间的小时数

发布时间:2020-12-15 22:33:42 所属栏目:百科 来源:网络整理
导读:我在一个场景中运行,我需要找到当前时段的总小时数.我有一个时间和TimeOut日期时间,我希望得到员工在第二天晚上10点到凌晨4点之间工作的时间.我如何获得工作小时的输出. 我创建了一个像这样的扩展方法: public static decimal GetNightDifferentialValue(th
我在一个场景中运行,我需要找到当前时段的总小时数.我有一个时间和TimeOut日期时间,我希望得到员工在第二天晚上10点到凌晨4点之间工作的时间.我如何获得工作小时的输出.

我创建了一个像这样的扩展方法:

public static decimal GetNightDifferentialValue(this DailyTime dtr,Employee201 employee,PayrollSettings settings,IEnumerable<Holidays> holidays)
    {
        //know if the time out is greater than 10pm of the dtr
        //07-26-2016 14:00 - 07-27-2016 03:00
        //if time out i
        var days = Enumerable.Range(0,(int)(dtr.TimeOut - dtr.TimeIn).TotalHours + 1)
            .Select(i => dtr.TimeIn.AddHours(i))
            .Where(date => !(date.Hour >= 22)).Count();
        return days* employee.Rate;

    }

我的问题是在Where方法中如何过滤仅属于我的类别的小时数

解决方法

public static decimal GetNightDifferentialValue(this DailyTime dtr,IEnumerable<Holidays> holidays)
    {
        //know if the time out is greater than 10pm of the dtr
        //07-26-2016 14:00 - 07-27-2016 03:00
        //if time out i
        DateTime dayIn10pm = new DateTime(dtr.TimeIn.Year,dtr.TimeIn.Month,dtr.TimeIn.Day,22,0);
        DateTime dayAfter04am = dayIn10pm.Add(new TimeSpan(6,0));

        var hours = Enumerable.Range(0,(int)(dtr.TimeOut - dtr.TimeIn).TotalHours + 1)
            .Select(i => dtr.TimeIn.AddHours(i))
            .Where(date => (date > dayIn10pm && date <= dayAfter04am)).Count();
        return hours;

    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读