RRDD
RRDD

Reputation: 15

How to handle hours in date

I am trying to get number of batches passed deadline, and number of batches near deadline (2 hours before the deadline). Following can get me if I am one day behind deadline. But how to handle hours?

What is required?

Get number of batches passed deadline, and number of batches near deadline (2 hours before the deadline

Customers Table:

CustomerID;CustomerName;BatchDeadline 
1;AAA;1
2;BBB;2
3;CCC;2
4;DDD;3

*BatchDeadline: Number of days after BatchDate, if 2 means second day of BatchDate in Batches table.

Batches Table:

BatchID;CustomerID;BatchDate
1;1;2014-12-10 8:00
2;2;2014-12-10 8:10
3;2;2014-12-11 10:20
4;1;2014-12-12 11:10
5;3;2014-12-12 11:12
6;3;2014-12-12 11:22
7;3;2014-12-12 11:23

Code:

int passedDeadline = 0;
int nearDeadline = 0;

foreach(customer c in customers)
{
  foreach(Batch b in batches) 
  {
      if (b.BatchDate < b.BatchDate(c.BatchDeadline * -1)
            passedDeadline++;
  }
}

Upvotes: 0

Views: 70

Answers (1)

Guffa
Guffa

Reputation: 700262

Get the current time (don't get it in the loop as the current time changes all the time):

DateTime now = DateTime.Now;

Calculate the points in time that you want to compare with:

DateTime deadline = b.BatchDate.AddDays(c.BatchDate);
DateTime nearDeadline = deadline.AddHours(-2);

Now just compare the time from the table to the points in time:

if (now > deadline) {
  // passed the deadline
} else if (now >= nearDeadline) {
  // near the deadline
}

Upvotes: 1

Related Questions