vbadatetimetime

How to omit minutes when formatting a time?


The following code is perfect.

    Sub Macro1()
            
        Dim mySecond As Double
        mySecond = 75
            
        Dim myMinute As Double
        myMinute = mySecond / 86400
            
        'Output of the following code is 01:15 which is perfect.
        Debug.Print Format(myMinute, "nn:ss")
             
    End Sub

I want to shorten the above code by omitting the following lines.

        Dim myMinute As Double
        myMinute = mySecond / 86400

Is it possible?


Solution

  • Use TimeSerial which is exactly for this:

       Sub Macro1()
                
            Dim mySecond As Integer
            mySecond = 75
                
            'Output of the following code is 01:15 which is perfect.
            Debug.Print Format(TimeSerial(0, 0, mySecond), "nn:ss")
                 
        End Sub