DATE$: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 12: Line 12:
{{PageExamples}}
{{PageExamples}}
''Example:'' Displaying the weekday and current date.
''Example:'' Displaying the weekday and current date.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|PRINT}} {{Cl|DATE$}}  
{{Cl|PRINT}} {{Cl|DATE$}}
month$ = {{Cl|LEFT$}}({{Cl|DATE$}}, 2): M = {{Cl|VAL}}(month$)
month$ = {{Cl|LEFT$}}({{Cl|DATE$}}, 2): M = {{Cl|VAL}}(month$)
day$ = {{Cl|MID$}}({{Cl|DATE$}}, 4, 2): D = {{Cl|VAL}}(day$)
day$ = {{Cl|MID$ (function)|MID$}}({{Cl|DATE$}}, 4, 2): D = {{Cl|VAL}}(day$)
day$ = {{Cl|STR$}}(D)                  ' eliminate any leading zeros
day$ = {{Cl|STR$}}(D)                  ' eliminate any leading zeros
year$ = {{Cl|RIGHT$}}({{Cl|DATE$}}, 4): Y = {{Cl|VAL}}(year$)
year$ = {{Cl|RIGHT$}}({{Cl|DATE$}}, 4): Y = {{Cl|VAL}}(year$)
Line 35: Line 35:


{{Cl|DEFINT}} A-Z
{{Cl|DEFINT}} A-Z
{{Cl|FUNCTION}} WeekDay$ (M, D, Y)        
{{Cl|FUNCTION}} WeekDay$ (M, D, Y)
{{Cl|IF}} M < 3 {{Cl|THEN}} M = M + 12: Y = Y - 1  'add 12 to Jan - Feb month, -1 year
{{Cl|IF}} M < 3 {{Cl|THEN}} M = M + 12: Y = Y - 1  'add 12 to Jan - Feb month, -1 year
C = Y \ 100: Y = Y {{Cl|MOD}} 100          'split century and year number
C = Y \ 100: Y = Y {{Cl|MOD}} 100          'split century and year number
Line 41: Line 41:
S2 = (5 * Y) \ 4                    '4 year leap
S2 = (5 * Y) \ 4                    '4 year leap
S3 = 26 * (M + 1) \ 10              'days in months
S3 = 26 * (M + 1) \ 10              'days in months
WkDay = (S1 + S2 + S3 + D) {{Cl|MOD}} 7    'weekday total remainder  
WkDay = (S1 + S2 + S3 + D) {{Cl|MOD}} 7    'weekday total remainder
{{Cl|IF}} WkDay < 0 {{Cl|THEN}} WkDay = WkDay + 7  'Adjust negative results to 0 to 6
{{Cl|IF}} WkDay < 0 {{Cl|THEN}} WkDay = WkDay + 7  'Adjust negative results to 0 to 6
{{Cl|SELECT CASE}} WkDay
{{Cl|SELECT CASE}} WkDay
Line 53: Line 53:
{{Cl|END SELECT}}
{{Cl|END SELECT}}
WeekDay$ = day$
WeekDay$ = day$
{{Cl|END FUNCTION}} '' ''
{{Cl|END FUNCTION}}
{{CodeEnd}}
{{CodeEnd}}
{{small|Code by Ted Weissgerber}}
{{Small|Code by Ted Weissgerber}}
{{OutputStart}}
{{OutputStart}}
06-02-2010
06-02-2010
Today is Wednesday, June 2, 2010
Today is Wednesday, June 2, 2010
{{OutputEnd}}
{{OutputEnd}}




{{PageSeeAlso}}
{{PageSeeAlso}}
* [[TIME$]], [[IF...THEN]]
* [[TIME$]], [[IF...THEN]]
* [[VAL]], [[STR$]], [[MID$]], [[LEFT$]], [[RIGHT$]]
* [[VAL]], [[STR$]], [[MID$ (function)]], [[LEFT$]], [[RIGHT$]]
 




{{PageNavigation}}
{{PageNavigation}}

Latest revision as of 00:33, 26 February 2023

The DATE$ function returns the current computer date as a string in the format "mm-dd-yyyy".


Syntax

today$ = DATE$


Description

  • Returns the current computer date in the format "mm-dd-yyyy" (e.g., "12-25-2009").


Examples

Example: Displaying the weekday and current date.

PRINT DATE$
month$ = LEFT$(DATE$, 2): M = VAL(month$)
day$ = MID$(DATE$, 4, 2): D = VAL(day$)
day$ = STR$(D)                  ' eliminate any leading zeros
year$ = RIGHT$(DATE$, 4): Y = VAL(year$)
SELECT CASE M
   CASE 1: Moon$ = "January"
   CASE 2: Moon$ = "February"
   CASE 3: Moon$ = "March"
   CASE 4: Moon$ = "April"
   CASE 5: Moon$ = "May"
   CASE 6: Moon$ = "June"
   CASE 7: Moon$ = "July"
   CASE 8: Moon$ = "August"
   CASE 9: Moon$ = "September"
   CASE 10: Moon$ = "October"
   CASE 11: Moon$ = "November"
   CASE 12: Moon$ = "December"
END SELECT
PRINT "Today is " + WeekDay$(M, D, Y) + ", " + Moon$ + day$ + ", " + year$ + SPACE$(10)

DEFINT A-Z
FUNCTION WeekDay$ (M, D, Y)
IF M < 3 THEN M = M + 12: Y = Y - 1  'add 12 to Jan - Feb month, -1 year
C = Y \ 100: Y = Y MOD 100           'split century and year number
S1 = (C \ 4) - (2 * C) - 1           'century leap
S2 = (5 * Y) \ 4                     '4 year leap
S3 = 26 * (M + 1) \ 10               'days in months
WkDay = (S1 + S2 + S3 + D) MOD 7     'weekday total remainder
IF WkDay < 0 THEN WkDay = WkDay + 7  'Adjust negative results to 0 to 6
SELECT CASE WkDay
   CASE 0: day$ = "Sunday"
   CASE 1: day$ = "Monday"
   CASE 2: day$ = "Tuesday"
   CASE 3: day$ = "Wednesday"
   CASE 4: day$ = "Thursday"
   CASE 5: day$ = "Friday"
   CASE 6: day$ = "Saturday"
END SELECT
WeekDay$ = day$
END FUNCTION
Code by Ted Weissgerber
06-02-2010
Today is Wednesday, June 2, 2010


See also



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