get hour

J

Josh

I have a time field textbox. I need to get the hour to use in a
variable. I can't use 24 hour time.

What I need, is if the time is 9:00am, then put 9 in the variable. If
the time is 9:00pm, then put 21 in the variable.

I can just use
IF txtTimeField = 9:00am, then hourVar=9
else if txtTimeField=10:00am, then hourVar=10

and so on, but it seems like there should be a better way of doing
this other than using an IF with 24 IFs.........

I know, 24 hour format would work, but this will be used on other
peoples computers, who will not have 24 hour format set.

Thanks
 
D

Douglas J. Steele

There's an Hour function that will do exactly what you want, or you can use
DatePart with a parameter of "h", or, as Randall suggested, you can use the
Format function.

Hour and DatePart both return numeric values, Format will return the number
as a string. Depending on what you're going to do with the value, that can
make a difference.
 
K

Klatuu

I think this is what you are asking:
iif(hour(Me.txtTime) < 12, hour(Me.txtTime), hour(Me.txtTime)-12)

Note that if it is 12:00 AM then you will get back a 0. 9:00 AM and 9:00 PM
will both return 9.
 
D

Douglas J. Steele

Why would you think that's what he's looking for, when he clearly stated "If
the time is 9:00pm, then put 21 in the variable"?
 
K

Klatuu

I would think that because I misread the post as saying he did not want it to
return 21.
Next time I see you have misread a post, Douglas, I will be happy to reply
with an equally smartass comment.
 
J

Josh

Thanks to all who answered, this works:

Me.hourVar = Format((Me.txtTimeField), "H")

Again, thanks!
 
Top