Skip to content

Date calculations

Adding and subtracting dates.

Q:  Martin writes:  I have these records written in two lines (dates, without separation in the European style (1030))

051007

051107

 

I then want to take the dates and subtract the dates from each other and get how much time has passed. An easy task with conform dates but this puzzles me.

Private Sub Report_Open(Cancel As Integer)

Dim strDte1 As String

Dim strDte2 As String

 

   strDte1 = Mid(Me![txtFraDatoTilDato], 1, 2) & “-” _

      & Mid(Me![txtFraDatoTilDato], 3, 2) & “-” _

      & Mid(Me![txtFraDatoTilDato], 5, 2)

   Debug.Print “Valid dato? ” & strDte1

   strDte2 = Mid(Me![txtFraDatoTilDato], 7, 2) _

      & “-” & Mid(Me![txtFraDatoTilDato], 9, 2) _

      & “-” & Mid(Me![txtFraDatoTilDato], 11, 2)

   Debug.Print “Valid dato? ” & strDte2

  

   If Me![txtFraDatoTilDato] <> 0 Then

      Me![txtCalTime] = strDte2 – strDte1

   Else

       Me![txtCalTime] = ” “

   End If

End Sub

 

A:  The problem is that you are working with strings — convert them into dates using the CDate function, then you can add or subtract, either with + and – or (more accurately) using the DateAdd function.

About this author