ECie
ECie

Reputation: 1477

Get the number of hours between time Period

I have run in a scenario where i need to find the total number of hours that falls on a current period. I Have a Time in and TimeOut datetime which i want to get the hours where the employee worked between 10pm-4am the following day. How would i get the output of hours worked.

I Created an Extension method like this:

 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 in the Where Method how can i Filter the hours that only fall on my category

Upvotes: 4

Views: 2564

Answers (6)

Ciro Corvino
Ciro Corvino

Reputation: 2128

 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;

 }

Upvotes: 2

ECie
ECie

Reputation: 1477

Get what I want but is not that good looking solution here is my solution:

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();
        }

here is the demo Demo

Upvotes: 0

Hari Prasad
Hari Prasad

Reputation: 16956

I see the problem is only with filtering, I would suggest compare Date part to determine is it next Date, if it is next date Look for TimeOfDay to compare 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();

Check this Demo

Upvotes: 2

kattav mauk
kattav mauk

Reputation: 56

  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
    }

Upvotes: 0

Kevin Gosse
Kevin Gosse

Reputation: 39007

I think this does what you want:

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;

Basically, we first normalize the dates:

  • If the employee got here before 10PM, consider he was there at 10PM
  • If the employee left after 4AM, consider he left at 4AM

From there, we just have to subtract the two dates, and handle the special case where the new timeIn is greater than the new timeOut (if, for instance, he worked from 2PM to 5PM). In which case it means there's no time between 10PM and 4AM, so we just return 0.

Note that this algorithm doesn't handle the case where the employee works more than 24 hours straight.

Upvotes: 0

Jishnu KM
Jishnu KM

Reputation: 234

i think this is your answer

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

Upvotes: -2

Related Questions