rel0aded0ne
rel0aded0ne

Reputation: 461

DateAdd() function not working properly

I want to use DateAdd() to calculate a new time but the results are confusing to me.

Dim lstZeit As Date 'lstZeit is 20:00:00 (8pm) 
Dim DatumEnd As Date

DatumEnd = DateAdd("h", 4, lstZeit) 

The Result of DatumEnd is 31.12.1899 and not 24:00:00

I think there is a problem with the calculation. If i try 09:00:00 instead of 20:00:00 the result is correct (13:00:00).

Upvotes: 0

Views: 840

Answers (1)

Gustav
Gustav

Reputation: 56026

The Result of DatumEnd is 31.12.1899 and not 24:00:00

That result is correct as there is no time of 24:00:00. 24 hours is 1 day, so your value is 1899-12-30 plus (20 + 4) hours => 1899-12-31.

If you wish to display extended count of hours, use a function like this:

Public Function FormatHourMinute( _
  ByVal datTime As Date, _
  Optional ByVal strSeparator As String = ":") _
  As String

' Returns count of days, hours and minutes of datTime
' converted to hours and minutes as a formatted string
' with an optional choice of time separator.
'
' Example:
'   datTime: #10:03# + #20:01#
'   returns: 30:04
'
' 2005-02-05. Cactus Data ApS, CPH.

  Dim strHour       As String
  Dim strMinute     As String
  Dim strHourMinute As String

  strHour = CStr(Fix(datTime) * 24 + Hour(datTime))
  ' Add leading zero to minute count when needed.
  strMinute = Right("0" & CStr(Minute(datTime)), 2)
  strHourMinute = strHour & strSeparator & strMinute

  FormatHourMinute = strHourMinute

End Function

Upvotes: 1

Related Questions