DATE$()
Jump to navigation
Jump to search
![]() |
![]() |
![]() |
![]() |
Description
- This function returns today's date as a string or returns a numeric value of the number of days passed since Jan 1, 1901. The numeric value allows using date$() for math operations.
- Additionally, one form of date$() returns a string value of any date given.
- See also TIME$()
Syntax
Today's date
These forms return a STRING with today's date formatted as shown.
date$() | spaces | Sep 21, 2010 | |
date$("mm/dd/yyyy") | 09/21/2010 | ||
date$("mm/dd/yy") | 09/21/10 | ||
date$("yyyy/mm/dd") | 2010/09/21 |
The last form is often used for sorting.
Days passed
These forms return the NUMBER of days passed from Jan 1, 1901 until today, or until the date given.
date$("days") | spaces | 40075 | |
date$("09/11/2001") | 36778 |
Any date
This form returns a string representation of any date, given the number of days since 1/1/1901.
date$(40075) | spaces | 09/21/2010 |
Additional formats for Run BASIC
date$("dd") | spaces | return day of month padded to 2 digits with leading zeros (eg. 07) |
date$("ddd") | return abbreviated day of the week (eg. Wed) | |
date$("dddd") | return full day of the week (eg. Wednesday) | |
date$("mm") | return month number padded to 2 digits with leading zeros (eg. 03) | |
date$("mmm") | return abbreviated month name (eg. Mar) | |
date$("mmmm") | return full month name (eg. March) | |
date$("yy") | return last two digits of the year (eg. 07) | |
date$("yyyy") | return year and century (eg. 2007) |
Hints
- The result of date$() can be assigned to a variable. e.g. d$ = date$()
- For database use, store dates in numeric format to save space and to make the dates immediately available for math operations. e.g. Searching records to find the newest or oldest date.
- When using date$() with an included date, the date can take any of these formats:
- date$("4/1/02")
- date$("1/1/1901")
- date$("April 1, 2002")
- date$("Apr 1, 2002")
- If only the last 2 digits of year are given, the first digits are assumed to be 20. e.g. "4/1/02" = "4/1/2002"
- You can use date$ to easily check if entered date is valid:
- print date$("09/31/2010")<> 0 '0 means invalid date
- print date$("09/29/2010")<> 0 '1 means valid date
- See Useful Functions
Example
Getting today's date
print date$() print date$("mm/dd/yyyy") ' The date$() result can be assigned to a variable. d$ = date$("mm/dd/yyyy") print d$
Date$() math
' A simple use of math with date$(), and showing use of ' numeric variables holding the result of date$(). input "Please enter your birthday as mm/dd/yyyy: ";bd$ born = date$(bd$) ' returns numeric format of birthday today = date$("days") ' returns numeric form of today ' Do the math... daysOld = today - born print "Today, you are "; daysOld; " days old!" ' Check if correct result print "That is "; daysOld/365; " years." end
Useful Procedures
Check for Leap Year
Function IsLeapYear(year) ' returns 1, if the specified year is a leap year, ' 0 otherwise. year must include the century as in 1980 IsLeapYear = date$("2/29/"; year) <> 0 End Function
Check for Valid Date
Function IsValidDate(d$) ' returns 1, if the date represented by d$ is valid, 0 if not IsValidDate = date$(d$) <> 0 End Function
Day of Week from Days
This takes the day as the number of days since Jan 1st, 1901
'examples of usage print weekDayFrDay$(date$("days")) 'now print weekDayFrDay$(date$("days")+1) 'tomorrow print weekDayFrDay$(date$("days")-7) 'week ago print date$(40000), weekDayFrDay$(40000) 'some random date function weekDayFrDay$(d) w = (d + 2) mod 7 weekDayFrDay$ = word$("Sunday Monday Tuesday Wednesday Thursday Friday Saturday", w+1) end function
Day of Week from Date
This takes the day as a date.
'examples of usage print weekDayfrString$("09/32/2010") 'invalid date print weekDayfrString$("09/29/2010") 'valid date function weekDayfrString$(d$) d = date$(d$) if d = 0 then weekDayfrString$ = "Invalid date ";d$ else w = (d + 2) mod 7 weekDayfrString$ = word$("Sunday Monday Tuesday Wednesday Thursday Friday Saturday", w+1) end if end function