by Gabriel Tavares de Oliveira Castellani (5 Submissions)
Category: String Manipulation
Compatability: Visual Basic 5.0
Difficulty: Unknown Difficulty
Originally Published: Thu 26th August 2004
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Formats a number (may be an amount of seconds, minutes, etc) into hh:nn or hh:nn:ss, using 24 hour convention.
API Declarations
'***********************************************************
' Formats a number as an hour (hh:mm ou hh:mm:ss)
'
' If FORMATO is equal to:
' 0: accepts VALOR as m, mm, hmm, hhmm, h:m, h:mm, hh:mm
' and converts to hh:mm (00:00)
' 1: accepts VALOR in minutes and converts to hh:mm.
' Ex.: Valor=123. Result=02:03
' 2: accepts VALOR as ss, mss, mmss, m:ss, mm:ss, hmmss,
' h:m:s, h:m:ss, h:mm:ss, hhmmss, hh:mm:ss, h:m, h:mm,
' h:mm and converts to hh:mm:ss
' 3: accepts VALOR as seconds and converts to hh:mm:ss
' Ex.: Valor=5148. Resposta=01:25:48
'
' If Roda24Horas is TRUE, dunno allow hours greater than 23
' Ex.: 25:10:18 will be converted to 01:10:18
'
' Ex.: FormataHora(107599, 3) -> 05:53:19
' FormataHora(107599, 3, False) -> 29:53:19
' FormataHora(107599, 3, False, True) -> 29h 53m 19s
'***********************************************************
' Função FormataHora
' Gabriel Tavares de Oliveira Castellani
' Faz a formatação de valor como hora (hh:mm ou hh:mm:ss)
' conforme Formato.
'
' Para Formato igual a:
' 0: recebe valor como m, mm, hmm, hhmm, h:m, h:mm, hh:mm
' e converte para hh:mm (00:00)
' 1: recebe valor em minutos e transforma para hh:mm.
' Ex.: Valor=123. Resposta=02:03
' 2: recebe valor como ss, mss, mmss, m:ss, mm:ss, hmmss,
' h:m:s, h:m:ss, h:mm:ss, hhmmss, hh:mm:ss, h:m, h:mm,
' h:mm e transforma para hh:mm:ss
' 3: recebe valor em segundos e transforma para hh:mm:ss
' Ex.: Valor=5148. Resposta=01:25:48
'
' O parâmetro Roda24Horas não permite que a hora seja
' superior a 23. Se for, faz com que a hora seja convertida
' para estar entre 00 e 23. Ex.: 25:10:18 fica 01:10:18
Function FormataHora(ByVal Valor As String, Optional ByVal Formato As enumFormato = 0, Optional ByVal Roda24horas As Boolean = True, Optional ByVal HMS As Boolean = False) As String
Dim ebolHM As Boolean 'Se verdadeiro, formata como 0. Se falso, como 2
Dim estrValor As String
Dim estrH As String, estrM As String, estrS As String, estrCS As String
Dim eintPos As Integer
Dim eintAux As Integer
estrValor = Valor
Select Case Formato
Case 0
ebolHM = True
If InStr(Valor, ":") > 0 Then
estrH = Format$(Val(Left$(Valor, InStr(Valor, ":") - 1)), "00")
estrM = Format$(Val(Right$(Valor, Len(Valor) - InStr(Valor, ":"))), "00")
Else
Select Case Len(Valor)
Case 0
estrH = "00"
estrM = "00"
Case 1, 2
estrH = "00"
estrM = Format$(Val(Valor), "00")
Case 3, 4
estrH = Format$(Val(Left$(Valor, Len(Valor) - 2)), "00")
estrM = Format$(Val(Right$(Valor, 2)), "00")
End Select
End If
Case 1
estrH = Format$(DivInt(Val(Valor), 60), "00")
estrM = Format$(Abs(Val(Valor)) Mod 60, "00")
ebolHM = True
Case 2
ebolHM = False
eintPos = InStr(Valor, ":")
If eintPos > 0 Then
estrH = Format$(Val(Left$(Valor, eintPos)), "00")
Valor = Right$(Valor, Len(Valor) - eintPos)
eintPos = InStr(Valor, ":")
If eintPos > 0 Then
estrM = Format$(Val(Left$(Valor, eintPos - 1)), "00")
estrS = Format$(Val(Right$(Valor, Len(Valor) - eintPos)), "00")
Else
estrM = Format$(Val(Valor), "00")
End If
Else
Select Case Len(Valor)
Case 0
estrH = "00"
estrM = "00"
estrS = "00"
Case 1, 2
estrH = "00"
estrM = "00"
estrS = Format$(Val(Valor), "00")
Case 3, 4
estrH = "00"
estrM = Format$(Val(Left$(Valor, Len(Valor) - 2)), "00")
estrS = Format$(Val(Right$(Valor, 2)), "00")
Case Else
estrH = Format$(Val(Left$(Valor, Len(Valor) - 4)), "00")
estrM = Format$(Val(Mid$(Valor, Len(Valor) - 4, 2)), "00")
estrS = Format$(Val(Right$(Valor, 2)), "00")
End Select
End If
Case 3
ebolHM = False
estrH = Format$(DivInt(Val(Valor), 3600), "00")
estrM = Format$(DivInt((Abs(Val(Valor)) - (Abs(Val(estrH)) * 3600)), 60), "00")
estrS = Format$(Abs(Val(Valor)) - (Abs(Val(estrH)) * 3600) - (Abs(Val(estrM)) * 60), "00")
Case 4
ebolHM = False
estrCS = Left$(Right$(Valor, Len(Valor) - InStr(Valor, ",")), 2)
estrH = Format$(DivInt(Val(Valor), 3600), "00")
estrM = Format$(DivInt((Abs(Val(Valor)) - (Abs(Val(estrH)) * 3600)), 60), "00")
estrS = Format$(Abs(Val(Valor)) - (Abs(Val(estrH)) * 3600) - (Abs(Val(estrM)) * 60), "00") & "." & estrCS
End Select
'Verificando a integridade dos dados e ajustando os erros de hora
If Val(estrS) >= 60 Then
eintAux = DivInt(Val(estrS), 60)
estrM = Format$(Val(estrM) + eintAux, "00")
estrS = Format$(Val(estrS) Mod 60, "00")
End If
If Val(estrM) >= 60 Then
eintAux = DivInt(Val(estrM), 60)
estrH = Format$(Val(estrH) + eintAux, "00")
estrM = Format$(Val(estrM) Mod 60, "00")
End If
If Roda24horas Then estrH = Format$(Val(estrH) Mod 24, "00")
'Enviando a resposta...
If HMS Then
If ebolHM Then
FormataHora = estrH & "h " & estrM & "m"
Else
FormataHora = estrH & "h " & estrM & "m " & estrS & "s"
End If
Else
If ebolHM Then
FormataHora = estrH & ":" & estrM
Else
FormataHora = estrH & ":" & estrM & ":" & estrS
End If
End If
End Function
No comments have been posted about Formats a number (may be an amount of seconds, minutes, etc) into hh:nn or hh:nn:ss, using 24 hour . Why not be the first to post a comment about Formats a number (may be an amount of seconds, minutes, etc) into hh:nn or hh:nn:ss, using 24 hour .