Calculating Elapsed Minutes when changing from PM to AM

D

davjoh123

Search many posts but not finding the right answer. A lot of posts
suggest the datediff function or subtracting the start time from the
end time and multiply by 24 and 60. This does nothing for the
situation were the time changes from pm to am.

Is there a good answer?



'If IsNull(Me.Duration) Then
If Not IsNull(Me.SetEndTime) And Not IsNull(Me.SetStartTime)
Then

'Hour(Me.SetEndTime - Me.SetStartTime) * 60 +
' Me.Duration = (DateDiff("n", Me.SetStartTime,
Me.SetEndTime))
'stTime = CDbl(Me.SetStartTime)
'endTime = CDbl(Me.SetEndTime)
'dblDuration = Round((endTime - stTime) * 24 * 60, 0)

Me.Duration = Round((Me.SetEndTime - Me.SetStartTime) * 24
* 60, 0)


End If
'End If
 
P

Piet Linden

Search many posts but not finding the right answer. A lot of posts
suggest the datediff function or subtracting the start time from the
end time and multiply by 24 and 60. This does nothing for the
situation were the time changes from pm to am.

Is there a good answer?

 'If IsNull(Me.Duration) Then
        If Not IsNull(Me.SetEndTime) And Not IsNull(Me.SetStartTime)
Then

        'Hour(Me.SetEndTime - Me.SetStartTime) * 60 +
           ' Me.Duration = (DateDiff("n", Me.SetStartTime,
Me.SetEndTime))
            'stTime = CDbl(Me.SetStartTime)
            'endTime = CDbl(Me.SetEndTime)
            'dblDuration = Round((endTime - stTime) * 24 * 60, 0)

            Me.Duration = Round((Me.SetEndTime - Me.SetStartTime) * 24
* 60, 0)

        End If
    'End If

Sounds like a lot of unnecessary work. use DateDiff("n",stTime,
endTime)
Doesn't something like this work for you?
?round(datediff("n",#6/22/2009 8:15 PM#,now)/60,2) & " hrs"
22.43 hrs
 
D

davjoh

Sounds like a lot of unnecessary work.  use DateDiff("n",stTime,
endTime)
Doesn't something like this work for you?
?round(datediff("n",#6/22/2009 8:15 PM#,now)/60,2) & " hrs"
22.43 hrs

If you go from pm to am the result is incorrect. as an example 11:40
PM to 12:12 AM gives a result of 14?? when it should be 32.
 
J

JimBurke via AccessMonster.com

Are these fields time-only, or are they date-time stamps? If they have date
and time, you should always be able to just subtract the two using DateDiff.
If they are time only that's a different story. If they are time only you can
only get valid results if you know they are one day apart (like in your
example), othersie how would oyu know how many days fall between? As long as
you know the times are never more than a day apart then something like this
should work:

If SetEndTime < SetStartTime then
SetEndTime = SetEndTime+ 1
End If
Duration = DateDiff("n", SetStartTime, SetEndTime)

I may have the order of the two times wrong in datediff - offhand I forget
which is which!

Adding 1 adds one day to the value - even if you only have times, Access
still treats it like a date and a time. This code assumes that if the end
time < start time then the times must have overlapped a day.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top