Time adding/sustracting

nkamp

Registered User.
Local time
Today, 14:40
Joined
Mar 2, 2007
Messages
15
Hello,

Is there simple function to calcualate with time like:

datediff("s", Timevalue(Time) - Timevalue("00:30:00"), rec("par1").value)

Has somebody an idea how to solve this?

thanks in advance.

Nico
 
here's a function I found on the web some time ago

Calculating the time difference between two dates

The TimeDiff() function calculates the difference between two dates, and returns that difference in one of the following formats:

Fraction of an hour - 1.01944444444444
Fraction of a minute - 61.0166666666667
Hours - 1
Hours and minutes - 1:1
Hours, minutes and seconds - 1:1:1
The function provides for an optional separator between each value. For example,

(1 hour, 25 minutes and 30 seconds) 1:25:30, 1-25-30, 1.25.30
'Author: © Copyright 2002 Pacific Database Pty Limited
' Graham R Seach gseach@pacificdb.com.au
' Phone: +61 2 9872 9594 Fax: +61 2 9872 9593
'
' You may freely use and distribute this code
' with any applications you may develop, on the
' condition that the copyright notice remains
' unchanged, and intact as part of the code. You
' may not sell or publish this code in any form
' without the express written permission of the
' copyright holder.
'
' Many thanks to Kennith Bennett for pointing out
' what happens in negative time.
'
'Description: This calculates the time difference
' between two dates, and returns that difference
' as a fraction of an hour, a fraction of a minute,
' the number of hours, the number of hours and minutes,
' or in the following formats:
' Hours
' Hours:Minutes
' Hours:Minutes:Seconds
'
'A user-defined separator can be supplied; the default
' being a semi-colon ( : ).
'
'Dependencies: None.
'
'Inputs: dteStart: The lower date.
' dteEnd: The upper date.
' iFormat: The enum value that specifies the
' output format. For example:
' dfHrFraction = fraction of an hour
' dfMinFraction = fraction of a minute
' dfH = Number of hours
' dfHM = Number of hours and minutes
' dfHMS = Number of hours, minutes and seconds
' vSeparator: User-defined separator used for dfH, dfHM,
' and dfHMS.

Public Enum DateFormat
dfHrFraction = 1
dfMinFraction = 2
dfH = 3
dfHM = 4
dfHMS = 5
End Enum

Public Function DiffTime(dteStart As Date, dteEnd As Date, iFormat As DateFormat, Optional vSeparator As Variant = ":") As Variant
Dim iHr As Integer
Dim iMin As Integer
Dim iSec As Integer
Dim dteTemp As Date
Dim vTemp As Variant
Dim bSwapped As Boolean

DiffTime = Null
bSwapped = False

'Check that both dates are valid
If Not IsDate(dteStart) Or Not IsDate(dteEnd) Then
DoCmd.Beep
MsgBox "You must supply valid dates.", vbOKOnly + vbExclamation, "Invalid date"
Exit Function
End If

'Check that dteStart < dteEnd
If dteStart > dteEnd Then
'dteStart > dteEnd. Swap them
dteTemp = dteStart
dteStart = dteEnd
dteEnd = dteTemp
bSwapped = True
End If

'Calculate the time differences
iHr = Abs(DateDiff("h", dteStart, dteEnd)) - _
IIf(Format(dteStart, "nnss") <= Format(dteEnd, "nnss"), 0, 1)
dteStart = DateAdd("h", iHr, dteStart)

iMin = Abs(DateDiff("n", dteStart, dteEnd)) - _
IIf(Format(dteStart, "ss") <= Format(dteEnd, "ss"), 0, 1)
dteStart = DateAdd("n", iMin, dteStart)

iSec = Abs(DateDiff("s", dteStart, dteEnd))

'Setup the output format
Select Case iFormat
Case 1 'Return as a fraction of an hour
vTemp = iHr + (iMin / 60) + (iSec / 360)
Case 2 'Return as a fraction of a minute
vTemp = (iHr * 60) + iMin + (iSec / 60)
Case 3 'Return as Hour
vTemp = iHr
Case 4 'Return as Hour:Minute
vTemp = iHr & vSeparator & iMin
Case 5 'Return as Hour:Minute:Second
vTemp = iHr & vSeparator & iMin & vSeparator & iSec
End Select

'Debug.Print iHr & ":" & iMin & ":" & iSec
DiffTime = IIf(bSwapped, "-", "") & vTemp
End Function


MVP Pages




Access 2000 and 2002 only!
 
Hello Allan57,

I think that this is not where I'm looking for. The only thing I want todo is substract a half an hour from the time now. Only calculating with time. Oke now I understand the misunderstanding. The thing I want is:

Time - Timevalue("00:30:00")
Let say the time is now 13:43:15, so the result shoult be 13:13:15.

Sorry for the wrong example.

Nico
 

Users who are viewing this thread

Back
Top Bottom