MS Access 2003: calcular y formatear el valor del tiempo transcurrido

Los datos expuestos en este blog, son solo de índole informativo. Por favor realiza siempre una copia de seguridad antes de realizar cualquier cambio en tu proyecto.

MS Access 2003: calcular y formatear el valor del tiempo transcurrido

Este tutorial de MSAccess explica cómo calcular y dar formato a los valores de tiempo transcurrido en Access 2003 (con capturas de pantalla e instrucciones paso a paso).

Pregunta: En Access 2003/XP/2000/97, ¿cómo formateo la hora en un informe para mostrar las horas y los minutos transcurridos?

Por ejemplo, 27 horas y 31 minutos se muestran como 27:31. Puedo hacer esto para valores de hasta 24 horas. Pero para cualquier tiempo superior a 24 horas, se descarta todo el día.

Respuesta: El “tiempo transcurrido” generalmente se calcula tomando la diferencia entre dos fechas. Por lo tanto, debe almacenar las fechas de “inicio” y “finalización” por separado y luego calcular el tiempo transcurrido.

Creamos una función que toma dos fechas y devuelve un tiempo transcurrido formateado.

Puede pegar la siguiente función en un módulo de Access y hacer referencia a ella como cualquier otra función de Access.

Public Function GetElapsedTime(pStart As Date, pEnd As Date) As String

   Dim LDate As Date
   Dim LYears As Integer
   Dim LMonths As Integer

   Dim LInterval As Double
   Dim Totalhours As Long
   Dim Totalminutes As Long
   Dim LDays As Long
   Dim LHours As Long
   Dim LMinutes As Long

   Dim LDisplay As String
   Dim LMthDisplay As String
   Dim LDayDisplay As String

   'Determine year portion of interval
   If Month(pEnd) < Month(pStart) Or (Month(pEnd) = Month(pStart) And Day(pEnd) < Day(pStart)) Then
      LYears = Year(pEnd) - Year(pStart) - 1
   Else
      LYears = Year(pEnd) - Year(pStart)
   End If

   LDate = DateAdd("yyyy", LYears, pStart)

   'Determine month portion
   If Day(pEnd) < Day(pStart) Then
      LMonths = DateDiff("m", LDate, pEnd) - 1
   Else
      LMonths = DateDiff("m", LDate, pEnd)
   End If

   LDate = DateAdd("m", LMonths, LDate)

   'Determine the elapsed days, hours, minutes, seconds portion
   LInterval = pEnd - LDate

   LDays = Int(CSng(LInterval))
   Totalhours = Int(CSng(LInterval * 24))
   Totalminutes = Int(CSng(LInterval * 1440))
   LHours = Totalhours Mod 24
   LMinutes = Totalminutes Mod 60

   'Pluralize months?
   If LMonths = 1 Then
      LMthDisplay = LMonths & " month, "
   Else
      LMthDisplay = LMonths & " months, "
   End If

   'Pluralize days?
   If LDays = 1 Then
      LDayDisplay = LDays & " day, "
   Else
      LDayDisplay = LDays & " days, "
   End If

   'Format display of results
   If LYears = 0 Then
      If LMonths = 0 Then
         If LDays = 0 Then
            LDisplay = Right("00" & LHours, 2)
            LDisplay = LDisplay & ":" & Right("00" & LMinutes, 2)
         Else
            LDisplay = LDayDisplay & Right("00" & LHours, 2)
            LDisplay = LDisplay & ":" & Right("00" & LMinutes, 2)
         End If
      Else
         LDisplay = LMthDisplay & LDayDisplay & Right("00" & LHours, 2)
         LDisplay = LDisplay & ":" & Right("00" & LMinutes, 2)
      End If
   Else
      If LYears = 1 Then
         LDisplay = LYears & " year, " & LMthDisplay & LDayDisplay
         LDisplay = LDisplay & Right("00" & LHours, 2) & ":" & Right("00" & LMinutes, 2)
      Else
         LDisplay = LYears & " years, " & LMthDisplay & LDayDisplay
         LDisplay = LDisplay & Right("00" & LHours, 2) & ":" & Right("00" & LMinutes, 2)
      End If
   End If

   'Return the full elapsed value
   GetElapsedTime = LDisplay

End Function