Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rounding to the nearest hour

Status
Not open for further replies.

JDPurtell

Programmer
Aug 13, 2001
9
0
0
US
Is there a function or procedure to round to the nearest hour. For example, if I had 1-Jan-2002 23:23:59 it would return 1-Jan-2002 23:00:00. Any help would be appreciated.
 
I am not sure if this is what you are looking for or not.

Private Sub Command1_Click()
Dim iHour As Long
Dim iMinute As Long

iHour = Hour(Now)
iMinute = Minute(Now)
If iMinute >= 30 Then
iHour = iHour + 1
End If
End Sub

Maybe? If you choose to battle wits with the witless be prepared to lose.
[machinegun][ducky]
 
I am obviously lost in simple land, so I'll just ask.

Why not just "FORMAT" the time as "Hour"? as in any / all of the below:

? Format(1/48, "Short Time")
00:30
? Format (Now, "H")
21

? Format (Now, "HH")
21

? Format (Now, "mm/dd/yy, H A/p")
06/05/02, 9 P
MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
As foada says, it is not entirely clear what you want. If all you need is the hour part of the time, then his example is probably the simplest way of getting it. If, on the other hand, you want to convert a whole date value to the nearest hour, something like this might be appropriate (based on the fact that a date is actually held as a floating point number, the decimal portion of which represents how many 1/24 of a day have passed):
[tt]
Private Function NearestHour(tTime As Date) As Date
NearestHour = Fix(tTime) + (((tTime - Fix(tTime)) * 24) Mod 24) / 24
End Function
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top