You can use date/time literals in CitectVBA code by enclosing them with the hash sign (#), in the same way you enclose string literals with double quotation marks (""). This is commonly known as declaring date constants.
For example, #2/6/10# represents the Australian date value of 2nd June, 2010 if the short date setting for the locale was set to d/MM/yyyy. The same date constant would represent the American date value of February 6, 2010 if the short date setting for the locale was set to MM/d/yyyy. See Formatting Date Values.
Note: The system locale settings are determined using Regional Settings in Windows Control Panel.
Similarly, you can compare a date/time value with a complete date/time literal:
If SomeDate > #3/6/99 1:20pm# Then
If you don't include a time in a date/time literal, CitectVBA sets the time part of the value to midnight (the start of the day). If you don't include a date in a date/time literal, CitectVBA sets the date part of the value to December 30, 1899.
CitectVBA accepts a wide variety of date and time formats in literals. These are all valid date/time values:
SomeDate = #3-6-93 13:20#
SomeDate = #March 27, 1993 1:20am#
SomeDate = #Apr-2-93#
SomeDate = #4 April 1993#
In the same way that you can use the IsNumeric
function to determine if a Variant variable contains a value that
can be considered a valid numeric value, you can use the
IsDate
function to determine if a
variant contains a value that can be considered a valid date/time
value. You can then use the CDate
function to convert the value into a date/time value.
For example, the following code tests the Text
property of a text box with IsDate
.
If the property contains text that can be considered a valid date,
CitectVBA converts the text into a date and computes the days left
until the end of the year:
Dim SomeDate, daysleft
If IsDate(Text1.Text) Then
SomeDate = CDate(Text1.Text)
daysleft = DateSerial(Year(SomeDate) + _
1, 1, 1) - SomeDate
Text2.Text = daysleft & " days left in the year."
Else
MsgBox Text1.Text & " is not a valid date."
End If
See Also