BBC BASIC for Windows
« TIME$-- convert the current time to 12hr »

Welcome Guest. Please Login or Register.
Apr 5th, 2018, 9:59pm



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

If you require a dump of the post on your message board, please come to the support board and request it.


Thank you Conforums members.

BBC BASIC for Windows Resources
Online BBC BASIC for Windows documentation
BBC BASIC for Windows Beginners' Tutorial
BBC BASIC Home Page
BBC BASIC on Rosetta Code
BBC BASIC discussion group
BBC BASIC for Windows Programmers' Reference

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: TIME$-- convert the current time to 12hr  (Read 198 times)
michael
Senior Member
ImageImageImageImage


member is offline

Avatar




PM


Posts: 335
xx TIME$-- convert the current time to 12hr
« Thread started on: Nov 25th, 2017, 03:34am »

I am sure someone will come up with a better example, but for now if you want to extract the current time and convert the 24 hour clock to 12 hour clock then I think I did it correctly.. If not let me know.. Thanks
Code:
      MODE 8
      REPEAT
        t$= RIGHT$(TIME$,8)
        h$= LEFT$(t$,2)
        m$= MID$(t$,4,2)
        s$= RIGHT$(t$,2)
        LET std%=VAL(h$)
        CASE std% OF
          WHEN 13 :h$="01"
          WHEN 14 :h$="02"
          WHEN 15:h$="03"
          WHEN 16:h$="04"
          WHEN 17:h$="05"
          WHEN 18:h$="06"
          WHEN 19:h$="07"
          WHEN 20:h$="08"
          WHEN 21:h$="09"
          WHEN 22:h$="10"
          WHEN 23:h$="11"
          WHEN 00:h$="12"
        ENDCASE
  
        PRINT h$+":"+m$+":"+s$
        WAIT 10
        CLS
      UNTIL FALSE
      END
 
User IP Logged

I like making program generators and like reinventing the wheel
DDRM
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 321
xx Re: TIME$-- convert the current time to 12hr
« Reply #1 on: Nov 26th, 2017, 3:57pm »

Hi Michael,

Your code looks fine, though it could be more concise. Since ":" is not a valid member of a number, the VAL of your t$ will just be the hour, so you could have:
Code:
t$=RIGHT$(TIME$,8)
IF VAL(t$)=0 THEN t$="12"+MID$(t$,3)
IF VAL(t$)>12 THEN t$=STR$(VAL(t$)-12)+MID$(t$,3)
PRINT t$ 


Could you add am or pm, as appropriate?

Code:
t$=RIGHT$(TIME$,8)+" am"
IF VAL(t$)=0 THEN t$="12"+MID$(t$,3)
IF VAL(t$)>12 THEN t$=STR$(VAL(t$)-12)+MID$(t$,3,6)+" pm"
PRINT t$ 


Best wishes,

D
User IP Logged

marsFS
New Member
Image


member is offline

Avatar




PM

Gender: Male
Posts: 6
xx Re: TIME$-- convert the current time to 12hr
« Reply #2 on: Nov 27th, 2017, 12:22am »

Very nice use of VAL, I've never used it that way so nice tip :)

I did spot one bug with the code as it will show incorrectly for 12 pm (noon).

Following code checks the time range and creates a suffix to append am or pm and I used h% to reduce repeated use of VAL and build the time string at the end but it makes no difference to the logic.

Code:
MODE 8

REPEAT
        t$=RIGHT$(TIME$,8)
  
        h%=VAL(t$)
        IF h%<12 THEN s$=" am" ELSE s$=" pm"
        IF h%>12 THEN h%-=12
        IF h%=0 THEN h%=12
  
        t$=RIGHT$("0"+STR$(h%),2)+MID$(t$,3)+s$
  
        PRINT TAB(0,0)t$
        WAIT 10
UNTIL FALSE
 
« Last Edit: Nov 27th, 2017, 03:48am by marsFS » User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

| |

This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls