TIME$: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
(Created page with "The '''TIME$''' Function returns a STRING representation of the current computer time in a 24 hour format. {{PageSyntax}} :: PRINT "Present time = "; '''TIME$''' * Returns the present computer time in hh:mm:ss 24 hour format: "19:20:33" * Uses 2 colon (:) separators between hours, minutes and seconds * Hour values range from "00" to "23" starting from midnite. * Minutes and seconds range from "00" to "59" * Continuous TIME$ calls may lag if a QBasic program is mi...")
 
No edit summary
 
(8 intermediate revisions by the same user not shown)
Line 3: Line 3:


{{PageSyntax}}
{{PageSyntax}}
:: PRINT "Present time = "; '''TIME$'''
: PRINT "Present time = "; '''TIME$'''




{{PageDescription}}
* Returns the present computer time in hh:mm:ss 24 hour format: "19:20:33"
* Returns the present computer time in hh:mm:ss 24 hour format: "19:20:33"
* Uses 2 colon (:) separators between hours, minutes and seconds
* Uses 2 colon (:) separators between hours, minutes and seconds
Line 13: Line 14:




{{PageExamples}}
''Example 1:'' A simple clock using [[DRAW]] with Turn Angle.
''Example 1:'' A simple clock using [[DRAW]] with Turn Angle.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|SCREEN}} 12
{{Cl|SCREEN}} 12
{{Cl|DO}}
{{Cl|DO}}
     {{Cl|CLS}}
     {{Cl|CLS}}
     t$ = {{Cl|TIME$}}: h = {{Cl|VAL}}(t$): m = {{Cl|VAL}}({{Cl|MID$}}(t$, 4, 2)): s = {{Cl|VAL}}({{Cl|MID$}}(t$, 7, 2))
     t$ = {{Cl|TIME$}}: h = {{Cl|VAL}}(t$): m = {{Cl|VAL}}({{Cl|MID$ (function)|MID$}}(t$, 4, 2)): s = {{Cl|VAL}}({{Cl|MID$ (function)|MID$}}(t$, 7, 2))
     {{Cl|PRINT}} t$
     {{Cl|PRINT}} t$
     {{Cl|CIRCLE}} {{Cl|STEP}}(0, 0), 200, 8
     {{Cl|CIRCLE}} {{Cl|STEP}}(0, 0), 200, 8
Line 26: Line 28:
     {{Cl|_DISPLAY}}
     {{Cl|_DISPLAY}}
     {{Cl|_LIMIT}} 1
     {{Cl|_LIMIT}} 1
{{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|INKEY$}} = {{Cl|CHR$}}(27) '' ''
{{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|INKEY$}} = {{Cl|CHR$}}(27)
{{CodeEnd}}
{{CodeEnd}}
{{small|Code by Galleon}}
{{Small|Code by Galleon}}
: Explanation: Note that [[VAL]](TIME$) can just return the hour number 0 to 23 as the read stops at the first colon.
: Explanation: Note that [[VAL]](TIME$) can just return the hour number 0 to 23 as the read stops at the first colon.




''Example 2:'' The following Function converts TIME$ to normal 12 hour AM-PM digital clock  format.
''Example 2:'' The following Function converts TIME$ to normal 12 hour AM-PM digital clock  format.
{{CodeStart}} '' ''
{{CodeStart}}
PRINT TIME$
PRINT TIME$
PRINT Clock$
PRINT Clock$
Line 39: Line 41:
{{Cl|FUNCTION}} Clock$
{{Cl|FUNCTION}} Clock$
hour$ = {{Cl|LEFT$}}(TIME$, 2): H% = {{Cl|VAL}}(hour$)
hour$ = {{Cl|LEFT$}}(TIME$, 2): H% = {{Cl|VAL}}(hour$)
min$ = {{Cl|MID$}}(TIME$, 3, 3)  
min$ = {{Cl|MID$ (function)|MID$}}(TIME$, 3, 3)
IF H% >= 12 THEN ampm$ = " PM" ELSE ampm$ = " AM"  
IF H% >= 12 THEN ampm$ = " PM" ELSE ampm$ = " AM"
IF H% > 12 THEN
IF H% > 12 THEN
   IF H% - 12 < 10 THEN hour$ = {{Cl|STR$}}(H% - 12) ELSE hour$ = {{Cl|LTRIM$}}({{Cl|STR$}}(H% - 12))
   IF H% - 12 < 10 THEN hour$ = {{Cl|STR$}}(H% - 12) ELSE hour$ = {{Cl|LTRIM$}}({{Cl|STR$}}(H% - 12))
ELSEIF H% = 0 THEN hour$ = "12"          ' midnight hour
ELSEIF H% = 0 THEN hour$ = "12"          ' midnight hour
ELSE : IF H% < 10 THEN hour$ = {{Cl|STR$}}(H%)  ' eliminate leading zeros  
ELSE : IF H% < 10 THEN hour$ = {{Cl|STR$}}(H%)  ' eliminate leading zeros
END IF
END IF
Clock$ = hour$ + min$ + ampm$
Clock$ = hour$ + min$ + ampm$
END FUNCTION '' ''
END FUNCTION
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}
{{OutputStart}}
Line 53: Line 55:
  2:13 PM
  2:13 PM
{{OutputEnd}}
{{OutputEnd}}
:''Explanation:'' When hours are less than 10 (but not 0), {{KW|STR$}}(H%) alone keeps a space ahead of the hour. For 2 digit hours, {{KW|LTRIM$}} is used to remove that leading space. For the hours of 10 AM to 12 PM, the hour {{KW|STRING}} value is passed from {{KW|LEFT$}}(TIME$, 2) at the beginning of the function.
:''Explanation:'' When hours are less than 10 (but not 0), [[STR$]](H%) alone keeps a space ahead of the hour. For 2 digit hours, [[LTRIM$]] is used to remove that leading space. For the hours of 10 AM to 12 PM, the hour [[STRING]] value is passed from [[LEFT$]](TIME$, 2) at the beginning of the function.




''See also:''
{{PageSeeAlso}}
* [[TIME$ (statement)]], [[TIMER]]
* [[TIMER (function)]]
* [[DATE$]], [[DATE$ (statement)]]  
* [[DATE$]], [[IF...THEN]]
* [[VAL]], [[STR$]], [[HEX]]  
* [[VAL]], [[STR$]], [[MID$ (function)]]
* [[LTRIM$]], [[MID$]], [[LEFT$]]
* [[LEFT$]], [[RIGHT$]]
* [[IF...THEN]]




{{PageNavigation}}
{{PageNavigation}}

Latest revision as of 00:46, 26 February 2023

The TIME$ Function returns a STRING representation of the current computer time in a 24 hour format.


Syntax

PRINT "Present time = "; TIME$


Description

  • Returns the present computer time in hh:mm:ss 24 hour format: "19:20:33"
  • Uses 2 colon (:) separators between hours, minutes and seconds
  • Hour values range from "00" to "23" starting from midnite.
  • Minutes and seconds range from "00" to "59"
  • Continuous TIME$ calls may lag if a QBasic program is minimized to the taskbar!


Examples

Example 1: A simple clock using DRAW with Turn Angle.

SCREEN 12
DO
    CLS
    t$ = TIME$: h = VAL(t$): m = VAL(MID$(t$, 4, 2)): s = VAL(MID$(t$, 7, 2))
    PRINT t$
    CIRCLE STEP(0, 0), 200, 8
    DRAW "c12ta" + STR$((h MOD 12) * -30) + "nu133"
    DRAW "c14ta" + STR$(m * -6) + "nu200"
    DRAW "c9ta" + STR$(s * -6) + "nu200"
    _DISPLAY
    _LIMIT 1
LOOP UNTIL INKEY$ = CHR$(27)
Code by Galleon
Explanation: Note that VAL(TIME$) can just return the hour number 0 to 23 as the read stops at the first colon.


Example 2: The following Function converts TIME$ to normal 12 hour AM-PM digital clock format.

PRINT TIME$
PRINT Clock$

FUNCTION Clock$
hour$ = LEFT$(TIME$, 2): H% = VAL(hour$)
min$ = MID$(TIME$, 3, 3)
IF H% >= 12 THEN ampm$ = " PM" ELSE ampm$ = " AM"
IF H% > 12 THEN
  IF H% - 12 < 10 THEN hour$ = STR$(H% - 12) ELSE hour$ = LTRIM$(STR$(H% - 12))
ELSEIF H% = 0 THEN hour$ = "12"          ' midnight hour
ELSE : IF H% < 10 THEN hour$ = STR$(H%)  ' eliminate leading zeros
END IF
Clock$ = hour$ + min$ + ampm$
END FUNCTION
14:13:36
 2:13 PM
Explanation: When hours are less than 10 (but not 0), STR$(H%) alone keeps a space ahead of the hour. For 2 digit hours, LTRIM$ is used to remove that leading space. For the hours of 10 AM to 12 PM, the hour STRING value is passed from LEFT$(TIME$, 2) at the beginning of the function.


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link