日付時刻 秒数や分数を時間や日付形式にするdd_hh:nn:ss

※より実装に近く表示させる為、コードの改行を避けています。スマホ等で閲覧される際は向きを変えてご覧ください。

※実装するバージョンによってはバージョンアップの仕様により動作しないコードもあります。実装には動作確認の上ご使用下さい。

Option Explicit


Function MakeTimeDateA(dblTime As DoubleAs String
'**********************************************
'秒数や分数を時間や日付形式にするdd_hh:nn:ss
'**********************************************
'引数dblTimeが秒の場合
'dd hh:nn:ss 形式の場合

    Dim Inds As Integer, Frms As String
    Dim Indn As Integer, Frmn As String
    Dim Indh As Integer, Frmh As String
    Dim Indd As Integer, Frmd As String

    Inds = dblTime Mod 60                '秒
    Indn = Round(dblTime \ 60) Mod 60    '分
    Indh = Round(dblTime \ 3600) Mod 24  '時
    Indd = Round(dblTime \ 86400)        '日

    Frms = Format(Inds, "0#")
    Frmn = Format(Indn, "0#")
    Frmh = Format(Indh, "0#")
    Frmd = Format(Indd, "0#")

MakeTimeDateA = Frmd & " " & Frmh & ":" & Frmn & ":" & Frms

'Mod 演算子
'2 つの数値の除算を行い、その剰余を返します。
'
'構文
'
'result = number1 Mod number2
'
'Mod 演算子の構文は、次の指定項目から構成されます。
'
'指定項目   内容
'result     必ず指定    任意の数値変数を指定します。
'number1    必ず指定    任意の数式を指定します。
'number2    必ず指定    任意の数式を指定します。
End Function


Private Sub testA()
Debug.Print MakeTimeDateA(86401)
Debug.Print MakeTimeDateA(86000)
Debug.Print MakeTimeDateA(8)
Debug.Print MakeTimeDateA(800000)
'返値
'01 00:00:01
'00 23:53:20
'00 00:00:08
'09 06:13:20
End Sub


Function MakeTimeDateB(dblTime As DoubleAs String
'**********************************************
'秒数や分数を時間や日付形式にするhh:nn:ss
'**********************************************
'引数dblTimeが秒の場合
'hh:nn:ss 形式の場合

    Dim Inds As Integer, Frms As String
    Dim Indn As Integer, Frmn As String
    Dim Indh As Integer, Frmh As String

    Inds = dblTime Mod 60                '秒
    Indn = Round(dblTime \ 60) Mod 60    '分
    Indh = Round(dblTime \ 3600)         '時

    Frms = Format(Inds, "0#")
    Frmn = Format(Indn, "0#")
    Frmh = Format(Indh, "0#")

MakeTimeDateB = Frmh & ":" & Frmn & ":" & Frms

End Function


Private Sub testB()
Debug.Print MakeTimeDateB(86401)
Debug.Print MakeTimeDateB(86000)
Debug.Print MakeTimeDateB(8)
Debug.Print MakeTimeDateB(800000)
'返値
'24:00:01
'23:53:20
'00:00:08
'222:13:20
End Sub
解説

剰余演算子は、数式 number1 を数式 number2 で除算し、その余りを演算結果 result として返します。このとき浮動小数点数は整数に丸められます。たとえば、次に示す式では、変数 A (演算結果 result) の値は 5 になります。

A = 19 Mod 6.7

通常、演算結果 result のデータ型は、result の値が整数であるかどうかに関係なく、バイト型 (Byte)、整数型 (Integer)、または長整数型 (Long)、あるいは、内部処理形式がバイト型、整数型、または長整数型のバリアント型 (Variant) になります。小数部分はすべて切り捨てられます。ただし、一方または両方の式が Null 値のときは、演算結果 result も Null 値になります。Empty 値を持つ式は、0 として扱われます。

 

 

 

2000年01月01日|[VBサンプルコード]:[日付時刻]