Sabtu, 12 November 2011

Convert Seconds to Days, Hours, Minutes, Seconds

' Duration
' By Quentin Zervaas [zervaas@strangeness.org]
' Use at will! Works as well as (better!) than mIRC's
' $duration function
' since it gives you multiple output options
' It's pretty easy to add more of your own
'depending on how you want it.
'
' Just add new cases, and define the strings accordingly
'(remember to allow spaces too.

Public Function Duration(TotalSeconds As Long, UpFormat As _
    Integer) As String
 
  ' Format = 0, 1, 2
  ' This determines the format of the time to be returned
  ' Type 0: 1d 4h 15m 47s
  ' Type 1: 1 day, 4:15:47
  ' Type 2: 1 day 4hrs 15mins 47secs
  ' Type else: Defaults to type 0
 
  Dim Seconds
  Dim Minutes
  Dim Hours
  Dim Days
  Dim DayString As String
  Dim HourString As String
  Dim MinuteString As String
  Dim SecondString As String
 
  Seconds = Int(TotalSeconds Mod 60)
  Minutes = Int(TotalSeconds \ 60 Mod 60)
  Hours = Int(TotalSeconds \ 3600 Mod 24)
  Days = Int(TotalSeconds \ 3600 \ 24)

  Select Case UpFormat
    Case 0
      DayString = "d "
      HourString = "h "
      MinuteString = "m "
      SecondString = "s"
    Case 1
      If Days = 1 Then DayString = " day, " _
      Else: DayString = " days, "
      HourString = ":"
      MinuteString = ":"
      SecondString = ""
    Case 2
      If Days = 1 Then DayString = " day " _
      Else: DayString = " days, "
      If Hours = 1 Then HourString = "hr " _
      Else: HourString = "hrs "
      If Minutes = 1 Then MinuteString = "min " _
      Else: MinuteString = "mins "
      If Seconds = 1 Then SecondString = "sec " _
      Else: SecondString = "secs"
    Case Else
      DayString = "d "
      HourString = "h "
      MinuteString = "m "
      SecondString = "s"
  End Select
 
  Select Case Days
    Case 0
      Duration = Format(Hours, "0") & HourString & _
            Format(Minutes, "00") & MinuteString & _
             Format(Seconds, "00") & SecondString
    Case Else
      Duration = Days & DayString & _
          Format(Hours, "0") & HourString & Format _
          (Minutes, "00") & MinuteString & _
           Format(Seconds, "00") & SecondString
    End Select
                
End Function

Tidak ada komentar:

Posting Komentar