View RSS Feed

Irfan_A

Produce time output in 12 hour format

Rate this Entry
by , 01-28-2010 at 10:03 AM (1047 Views)
In Liberty BASIC, and maybe in other BASIC language, time$() command will output current time of the system clock in 24 hour format.

Code:
print time$()  'time now as string "23:59:46"
In order to produces output in 12 hour format, we must do manipulating character. We have many ways to do these. I only submit only three.

#1. IF THEN
If hours smaller than or equal with 12, we set to AM time. Otherwise, we subtract hours with 12 and set to PM time.

Code:
time$=time$()            'set time now
hh=val(time$)            'set hours
mm.ss$=right$(time$,6)   'set minutes and seconds 

    if hh<=12 then               '24.00-12.00
        print hh;mm.ss$;" AM"
    else                         '12.00-24.00
        print hh-12;mm.ss$;" PM"
    end if
#2. SELECT CASE
In case hours smaller than or equal with 12, we set to AM time. In other case, we subtract hours with 12 and set to PM time.

Code:
time$=time$()            'set time now
hh=val(time$)            'set hours
mm.ss$=right$(time$,6)   'set minutes and seconds 

select case
        case hh<=12              '24.00-12.00
             print hh;mm.ss$;" AM"
        case hh>12               '12.00-24.00
             print hh-12;mm.ss$;" PM"
end select
#3. FUNCTION
In Liberty BASIC distribution has already AMPMTIME.bas example. Here, i will submit mine with different way but produce same. Function useful in order to code reusable in future.

Code:
print AmPmTime$(time$())

function AmPmTime$(time$)
    time$=time$()           'set time now
    hh=val(time$)           'set hours
    mm.ss$=right$(time$,6)  'set minutes and seconds
    if hh<=12 then          '24.00-12.00
        hh=hh
        AmOrPm$=" AM"
    else                    '12.00-24.00
        hh=hh-12
        AmOrPm$=" PM"
    end if
    AmPmTime$=hh;mm.ss$;AmOrPm$  'return Value Expression
end function

Thats' all.
Happy coding.



-Irfan Afifullah

Submit "Produce time output in 12 hour format" to Digg Submit "Produce time output in 12 hour format" to del.icio.us Submit "Produce time output in 12 hour format" to StumbleUpon Submit "Produce time output in 12 hour format" to Google

Comments

  1. kailas's Avatar
    often faced with such a conversion. Thank you.