Produce time output in 12 hour format
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.
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.Code:print time$() 'time now as string "23:59:46"
#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.
#2. SELECT CASECode: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
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.
#3. FUNCTIONCode: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
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











