Get the number of hours between time periods

I ran in a scenario where I need to find the total number of hours that fall for the current period. I have time and time, as well as the time that I want to get when the employees worked from 22 to 4 hours the next day. How do I get the result of hours worked.

I created an extension method, for example:

 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;

    }

My problem is where method, how can I filter hours that fall only in my category.

+4
source share
6 answers
 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
        DateTime dayIn10pm = new DateTime(dtr.TimeIn.Year, dtr.TimeIn.Month, dtr.TimeIn.Day, 22, 0, 0);
        DateTime dayAfter04am = dayIn10pm.Add(new TimeSpan(6,0,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;

    }
+1
source

, , Date , , Date, TimeOfDay Time

var t = TimeSpan.ParseExact("04:00:00", @"hh\:mm\:ss", CultureInfo.InvariantCulture);

var days = Enumerable.Range(0, (int)(dtr.TimeOut - dtr.TimeIn).TotalHours + 1)
        .Select(i => dtr.TimeIn.AddHours(i))
        .Where(date => (date.Date == TimeOut.Date && date.TimeOfDay <= t)  || date.Hour >= 22)
        .Count();

Demo

+2

, , :

if (timeOut.Hour > 4)
{
    timeOut = timeOut.Date.AddHours(4);
}

if (timeIn.Hour < 22)
{
    timeIn = timeIn.Date.AddHours(22);
}

if (timeIn > timeOut)
{
    // No overnight time
    return 0;
}

var difference = timeOut - timeIn;

return difference.TotalHours;

, :

  • 10 , , 10
  • 4 , , 4

, timeIn , timeOut (, , 2 5 ). , 10PM 4AM, 0.

, , 24 .

0
  private void CalculateTotalHour(string dtstartTime, string dtendTime)
    {

            DateTime d1 = new DateTime();
            d1 = Convert.ToDateTime(dtstartTime); DateTime d2 = new DateTime();
            d2 = Convert.ToDateTime(dtendTime);

            if (d1.Hour >= 12)
            {
                d1 = d1.AddDays(-1);
            }
            else if (d2.Hour >= 12)
            {
                d2 = d2.AddDays(1);
            }

          //  if (d2 < d1)
           //  MessageBox.Show("shift end time is lesser than shift start time", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
             TimeSpan ts = d2.Subtract(d1).Duration();
           // ts.ToString(@"hh\:mm")//total dur
    }
0

, , - :

var timeIn = new DateTime(2016, 7, 25, 14, 0, 0);
        var timeOut = new DateTime(2016, 7, 26, 5, 0, 0);

        if ((timeOut.Date - timeIn.Date).TotalDays >= 1)
        {
            var hrs12to4am = Enumerable.Range(0, (int)(timeOut - timeIn).TotalHours + 1)
                .Select(i => timeIn.AddHours(i)).Where(a => a.Hour < 4 && a.Date > timeIn).ToList();

            var hrsOverTen = Enumerable.Range(0, (int)(timeOut - timeIn).TotalHours + 1)
                .Select(i => timeIn.AddHours(i)).Where(a => a.Hour > 22).ToList();

        }
        else
        {
            var hrsOverTen = Enumerable.Range(0, (int)(timeOut - timeIn).TotalHours + 1)
                .Select(i => timeIn.AddHours(i)).Where(a => a.Hour > 22).ToList();
        }

0

,

int hours = (int)(dt2 - dt1).TotalHours;
-2
source

All Articles