Link to home
Start Free TrialLog in
Avatar of tlengnick
tlengnickFlag for United States of America

asked on

vb6 dateadd

I want to add a variable dim'd as a SINGLE to a specific time and have it return the subsequent time. It needs be able to handle fractions of an  hour.

For example, Add 1.5 hours to 9:00 AM and return 10:30 AM.

This should be simple, but I'm screwing it up. I'm using all variables to do this, like so:
Dim snglDuration as Single
Dim dStartTime, dEndTime as Date

dEndTime = Format(dateadd("h", snglDuration, dStartTime), "hh:mm AMPM")

it returns dEndTime = 10:00 AM

It's not recognizing the .5 of an hour... do I have to do this in minutes and convert?

Also, does it matter that I Dim like this:
Dim dStartTime, dEndTime as Date

instead of like this:
Dim dStartTime as Date, dEndTime as Date

?
Avatar of rockiroads
rockiroads
Flag of United States of America image

why dont you convert to minutes then do dateadd on minutes?
ASKER CERTIFIED SOLUTION
Avatar of rockiroads
rockiroads
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
if you have the time in hours only, you could multiply it by 60
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
1.5 hours can also be represented at 1/16 of a day.

dEndTime =  dStartTime + 1/16

Option Explicit

Private Sub Form_Load()
   Dim dateStart As Date
   Dim dateEnd As Date
   
   dateStart = Date + TimeSerial(10, 0, 0)
   dateEnd = dateStart + 1 / 16
   MsgBox dateStart & vbCrLf & dateEnd
End Sub

Open in new window

Avatar of tlengnick

ASKER

Thanks, guys!