1

So I have a dateTimePicker which is labeled as pickUpTime.

DateTime pickUpTime = dateTimePicker1.Value.Date.AddHours(pickUpTimePicker.Value.Hour);

Which lets the user select a date and time and assigns it to pickUpTime.

I am trying to write a statement that will execute and add an extra charge if the pickUpTime is in between a range of times. Say 00:00 and 06:59 or 18:00 and 23:59.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Mattski357
  • 25
  • 4

2 Answers2

0

You should make use of ticks

if (targetDt.Ticks > d1.Ticks && targetDt.Ticks < d2.Ticks)
{
    // targetDt is in between d1 and d2
}

where d1 and d2 are your specified times

Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
0

You can verify the TimeOfDay of one DateTime is between two TimeSpans:

TimeSpan lowerTime = new TimeSpan(0, 0, 0);
TimeSpan upperTime = new TimeSpan(6, 59, 0);
bool result = pickUpTime.TimeOfDay >= lowerTime
    && pickUpTime <= upperTime;
wizulus
  • 5,653
  • 2
  • 23
  • 40
  • Thanks! This is similar to what I was trying last night except when I tried to compare the pickUpTime to the lowerTime I left out pickUpTime.TimeOfDay and just tried pickUpTime. – Mattski357 Feb 27 '14 at 19:24