Using VBA/VBS to Create a Time from Seconds from Midnight number

Written by James McDonald

January 15, 2009

I have a Progress DB field that stores a time stamp in seconds from midnight format (1-86400 seconds). I Googled and only seemed to dig up examples from other languages.

This is my attempt in VBA/VBS Language. It seems to return the correct values.

Function ct(stamp)

    hours = Int(stamp / 3600)
    If Len(hours) = 1 Then hours = "0" & hours

    minutes = Int((stamp Mod 3600) / 60)
    If Len(minutes) = 1 Then minutes = "0" & minutes

    seconds = Int((stamp Mod 3600) Mod 60)
    If Len(seconds) = 1 Then seconds = "0" & seconds

    ct = hours & ":" & minutes & ":" & seconds

End Function

After working with Perl for a little bit I’m finding VBA/S lack of a sprintf style function is annoying. If anyone can come up with a better way of padding the zeros in the above example I would be interested.

This is the Perl example of calculating seconds from midnight as a time…

#!/usr/bin/perl -W

# seconds from midnight can be anywhere between
# 1-86400 (00:00:01 one second past mignight - 00:00:00 midnight the following day)

for (1..86400) {

print "seconds past midnight ", 
sprintf("%06d", $_) , 
" timestamp " , 
sprintf("%02d", ($_/(60*60))%24), ":", 
sprintf("%02d", ($_/60)%60) ,":" ,
sprintf("%02d", $_%60), 
"\n" ;

}

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.

You May Also Like…

Network speed test host to host

On Ubuntu / Debian apt-get install iperf3 On Windows download it from https://iperf.fr/iperf-download.php#windows Make...

Clear HSTS Settings in CHrome

Open chrome://net-internals/#hsts enter the domain in the query field and click Query to confirm it has HSTS settings...