-2
    Public void Fee()
    {
        TimeSpan span1 = TimeSpan.FromHours(dtmIn.Value.Hour);
        TimeSpan span2 = TimeSpan.FromHours(dtmOut.Value.Hour);
        TimeSpan span3 = TimeSpan.FromMinutes(dtmIn.Value.Minute);
        TimeSpan span4 = TimeSpan.FromMinutes(dtmOut.Value.Minute);
        TimeSpan span5 = span2.Subtract(span1) + span4.Subtract(span3);

        lblTotal.Text = (span5.TotalHours * 3).ToString("$#.00");

    }

I do not want the user to be able to be able to clock in during PM and clock out during AM(basically overnight working). Also, not allowing the clock out time being before the clock in time.

slugster
  • 49,403
  • 14
  • 95
  • 145
John
  • 81
  • 1
  • 4

3 Answers3

1

You should call new TimeSpan(hours, minutes, seconds: 0) and check whether the in TimeSpan is > the out TimeSpan.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Alright, I am slightly confused on this. This is my first time dealing with timespans. – John Nov 20 '11 at 03:18
  • 2
    @John It sounds to me like (as you have said), you are *really* new to programming. This isn't really a programming tutorial site; You should have a basic idea how to program, and have specific problems to solve, before you come here. It really sounds like you don't have a handle on many of the real basics. – Andrew Barber Nov 20 '11 at 03:47
  • This does not sound like the right solution to me. What about the case where the user works over 24 hours? Might not be likely, but it certainly isn't handled by the logic and it seems easy to handle using `DateTime`s, which the OP already has. – Adam Robinson Nov 20 '11 at 04:17
  • @AdamRobinson From what the OP said, that's not possible. He explicitly said that no one should be permitted to enter a start time in one day and an end time in another day. – Andrew Barber Nov 20 '11 at 04:40
  • @AndrewBarber: Even so, I'm not seeing how constructing a `TimeSpan` from the hour, minute, and second properties of an existing `DateTime` and comparing it to another one would be simpler than just comparing the two `DateTime`s in question. – Adam Robinson Nov 20 '11 at 18:04
0

It appears from your code sample that dtmIn and dtmOut are nullable DateTime variables. If so, all you have to do is this:

if (dtmIn.Value >= dtmOut.Value)
{
    //'in' time is equal to or greater than 'out' time
    ... show my error message ...
}

Of course you will need to ensure the DateTime? variables have a value (i.e. do appropriate error checking before using them in the expression).

slugster
  • 49,403
  • 14
  • 95
  • 145
0

You probably need to be a little more specific with your logic. Do you mean...

  • The user should be able to work overnight? If so, that means you need to check to make sure that the date they clocked in is the same as the date they clocked out. `

For example...

if (dtmIn.Value.Date != dtmOut.Value.Date) 
{ 
    ... 
}
  • The user should not be able to work more than 24 hours? If so, you should subtract the two dates and use the resulting TimeSpan to see how many days they worked.

For example...

if ((dtmOut.Value - dtmIn.Value).TotalDays > 1) 
{
    ... 
}

In neither case should you check the time explicitly. For one, if I worked 25 hours then my check out time would still be after the check in time.

Adam Robinson
  • 182,639
  • 35
  • 285
  • 343