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 *

You May Also Like…

How to Research a CPU Upgrade

How to Research a CPU Upgrade

Upgrade Time! Doing a lot of VMWare Workstation virtualization to create labs for self-study and training. Finding...