The compiler treats literals enclosed within number signs (# #) as Date. (#5.7.95# or #5.7.95 12:42:30#). GFA-BASIC 32 provides three predefined formats for date/time literals. These are not country dependent so a program can be used in different languages.
#dd.mm.yyyy#
#mm/dd/yyyy#
#yyyy-mm-dd#
The recognition of the proper format Day.Month.Year, Month/Day/Year, or Year-Month-Day depends on the separation mark. The Val()-function accepts all three formats, as well.
These formats are independent of your locale and your computer's date and time format settings. The reason for this restriction is that the meaning of your code should never change depending on the locale in which your application is running. Suppose you hard-code a Date literal of #3/4/1998# and intend it to mean March 4, 1998. In a locale that uses mm/dd/yyyy, 3/4/1998 compiles as you intend. But suppose you pass the code on the users in other countries. In a locale that uses dd/mm/yyyy, your hard-coded literal would compile to April 3, 1998. In a locale that uses yyyy/mm/dd, the literal would be invalid (April 1998, 0003) and cause a compiler error.
Despite the above, GFA-BASIC 32 allows date literals in other locales by using CDate:
Dim d As Date = CDate("22 Nov 1995")
You can use date/time literals in your code by enclosing them with the number sign (#), in the same way you enclose string literals with double quotation marks (""). For example, you can compare a Variant containing a date/time value with a literal date:
If SomeDate > #03/06/1993# Then
Similarly, you can compare a date/time value with a complete date/time literal:
If SomeDate > #03/06/1993 13:20:00# Then
If you do not include a time in a date/time literal, GFA-BASIC 32 sets the time part of the value to midnight (the start of the day).
GFA-BASIC 32 accepts a wide variety of date and time formats in string-based literals as well (although not in true literal form surrounded by #s). These are all valid date/time values:
Print CDate("3-6-93 13:20")
Print CDate("March 27, 1993 1:20am")
Print CDate("Apr-2-93")
Print CDate("4 April 1993")
A Date type is internally interpreted as a Double type, except with explicit or implicit conversion to string routines (Print, Str, and Format). You can perform calculations on date/time values. Adding or subtracting integers adds or subtracts whole days; adding or subtracting fractions adds or subtracts fractions of days (expressed in hours and minutes). Thus, adding 20, adds 20 days, and subtracting 1/24 subtracts one hour.
Print Date + 8 // Returns the date in 8 days
Print #24.12.2008# + 8
Print DmyToDate(24, 12, Year(Date)) - Date // Returns the remaining days to Christmas eve
Print DateSerial((Year(Date), 12, 24, )) - Date
Because the current year is appended automatically...
#24.12#
...is automatically converted by the IDE to the 24th December of the current year.
Important information on Date and Time:
Since GFA-BASIC 32 tries to maintain compatibility to VB, even when it is erroneous, you should use DateAdd and DateDiff- functions when using dates before December 1899.
Finally, you can check to see if a Date variable holds a valid date by using the IsDate function.
{Created by Sjouke Hamstra; Last updated: 18/09/2021 by James Gaite}