vb6 dateadd

tlengnick
tlengnick used Ask the Experts™
on
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

?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2006

Commented:
why dont you convert to minutes then do dateadd on minutes?
Top Expert 2006
Commented:
eg to add 1.5 hours

    Dim dEndTime As Date
    Dim dStartTime As Date
    Dim snglDuration As Single
   
   
    snglDuration = 90
    dStartTime = Now
   
    dEndTime = DateAdd("n", snglDuration, dStartTime)
   
    MsgBox dStartTime & vbCrLf & dEndTime

Top Expert 2006

Commented:
if you have the time in hours only, you could multiply it by 60
Should you be charging more for IT Services?

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

GrahamSkanRetired
Top Expert 2012
Commented:
The second argument of DateAdd is treated as an integer. In the Help text it is described as the number of intervals to be added to the date variable.

You could do a simple addition:

dEndTime =  dStartTime + snglDuration/24
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

Author

Commented:
Thanks, guys!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial