Date and Time

Date Literals

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")

Date Data Type

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:

  1. Date/Time data types do not have a suffix.
  2. Valid date values range from -647,434 (January 1, 100 A.D.) to 2,958,465 (December 31, 9999 A.D.).
  3. A date value of 0 represents December 30, 1899; dates prior to December 30, 1899 are stored as negative numbers.
  4. Note: Although a date variable can hold a date prior to 1st January 1601, GFABASIC's auto-conversion to string when printing the date does not work on dates earlier than this for Date variables - it does for those stored in Variants though...
  5. Valid time values range from .0 (00:00:00) to .99999 (23:59:59).
  6. The numeric value represents a fraction of one day: you can convert the numeric value into hours, minutes, and seconds by multiplying the numeric value by 24.
  7. Calculations involving Date variables do not take into account the calendar revisions prior to the switch to the Gregorian calendar, however, so calculations producing date values earlier than the year in which the Gregorian calendar was adopted (1752 in Britain and its colonies at that time; earlier or later in other countries) will be incorrect.
  8. Using two-digit year values is fraught with problems: Windows 98 has a base year of 1930 ,meaning that a two-digit year value of "30" is read as 1930, whereas Windows 10 has a base of 1950 (in some cases), meaning that "30" would be read as 2030. This can cause compatibility problems in a program ported to a later OS and so it is advised to stay away from two-didgit years if at all possible.

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}