Military time to Normal

 BBS: Inland Empire Archive
Date: 02-13-93 (17:35)             Number: 393
From: JIM LITTLE                   Refer#: NONE
  To: STEVE SPENCER                 Recvd: NO  
Subj: Military time to Normal        Conf: (2) Quik_Bas
SS>I know this is going to sound terribly simple but I'm new at QB.
SS>I have begun to write some simple door programs for the Bulletin Board I am
SS>going to be setting up.  I havn't been able to find out a way of taking the
SS>system time (in millitary form) and converting it to
SS>normal time (1-12) with
SS>a PM or AM on the end.  Can anyone help me with this.
SS>It doesn't nead to be
SS>set up for a door program, just a function that would do this and write the
SS>output to a veriable.


Hi, Steve:

        After quickly whipping up some code without really thinking
about it (something I do way too often.. :) ), I decided to test it to
see if it actually worked.  (And am I glad I did!)  It turns out there
were lots of special cases I completely ignored in my first version. But
anyway, here's a function that converts from military time standard
time, and hopefully without any bugs:


   ----- Cut Here --------

DECLARE FUNCTION StandardTime$ (MilitaryTime AS INTEGER)
CONST False = 0
CONST True = NOT False

CLS
0 :
INPUT "Enter Military Time: ", time%
IF time% = -1 THEN END
PRINT "Standard Time: "; StandardTime(time%)
PRINT
GOTO 0

FUNCTION StandardTime$ (MilitaryTime AS INTEGER)

DIM am AS INTEGER         'true if time is am, false if pm
DIM hours AS INTEGER      'number of hours (in standard time)
DIM minutes AS INTEGER    'number of minutes
DIM temptime AS STRING    'standard time representation of MilitaryTime

'split MilitaryTime up into hours and minutes
hours = MilitaryTime \ 100  '(note: integer division)
minutes = MilitaryTime MOD 100

'determine if am or pm, and convert hours to 1-12
IF hours < 12 OR hours = 24 THEN  'am
   am = True
   IF hours = 24 OR hours = 0 THEN
      hours = 12
   END IF
ELSE  'pm  (hours >= 12, but not equal to 24)
   am = False
   IF hours <> 12 THEN
      hours = hours - 12
   END IF
END IF

'create string
'Note: LTRIM$(STR$(..)) combo converts number to string, and strips
'       unneeded spaces that QB throws in

'add hours & colon to string
temptime = LTRIM$(STR$(hours)) + ":"

'add minutes to string   If minutes less than 10, pad with leading 0
IF minutes > 9 THEN
   temptime = temptime + LTRIM$(STR$(minutes))
ELSE  'minutes < 10
   temptime = temptime + "0" + LTRIM$(STR$(minutes))
END IF

'add 'am' or 'pm' to string
IF am THEN
   temptime = temptime + " am"
ELSE
   temptime = temptime + " pm"
END IF

'return it!
StandardTime = temptime

END FUNCTION

   ----- End Snip --------


Hope this helps...  If you have any problems, feel free to ask.

-Jim


 * SLMR 2.1a * Nothing is so smiple that it can't get screwed up.


--- WM v2.06/91-0012
 * Origin: Com-Dat BBS  Hillsboro, OR.  HST (503) 681-0543 (1:105/314)
Outer Court
Echo Basic Postings

Books at Amazon:

Back to BASIC: The History, Corruption, and Future of the Language

Hackers: Heroes of the Computer Revolution (including Tiny BASIC)

Go to: The Story of the Math Majors, Bridge Players, Engineers, Chess Wizards, Scientists and Iconoclasts who were the Hero Programmers of the Software Revolution

The Advent of the Algorithm: The Idea that Rules the World

Moths in the Machine: The Power and Perils of Programming

Mastering Visual Basic .NET