Date

From Real Software Documentation

Jump to: navigation, search
Class (inherits from Object)

A Date object stores the number of seconds since 12:00 AM, January 1, 1904. Properties of a Date enable you to get and set a day value only, a date/time, or only a time.


Properties
AbbreviatedDate LongDate Second
Day LongTime ShortDate
DayOfWeek Minute ShortTime
DayOfYear Month TotalSeconds
GMTOffset SQLDate WeekOfYear
Hour SQLDateTime Year


Constructors

Date.Constructor()


Date.Constructor(CopyDate as Date)


Date.Constructor(Year as Integer, Month as Integer = 1, Day as Integer = 1, hour as Integer = 0, minute as Integer = 0, second as Integer = 0)


Date.Constructor(Year as Integer, Month as Integer = 1, Day as Integer = 1, hour as Integer = 0, minute as Integer = 0, second as Integer = 0, GMTOffset as double)



Notes

When you create and instantiate a Date object, it is initialized to the current date and time. Therefore the order of assignment is important: when dealing with a day that exists in one month when the current system date is in another (such as December 31 when you're in November), setting the Day before the month can result in an unexpected (here 12/01/2011) or even invalid date. Not setting the year before entering Feb 29 will cause oddities if the current year is not a leap year.

If you try to set the date or date/time and the format is incorrect, Real Studio will raise an UnsupportedFormatException.

The date properties of FolderItems can be accessed via the CreationDate and ModificationDate properties of FolderItem objects. You can get the current date and time by creating a new date and reading the values of the Year, Month, Day, Hour, Minute, and Second properties.

In the following code:

Dim v as Variant
Dim d as Date
d=New Date
v=d


What is actually happening is that the Variant stores the value of the TotalSeconds property as a Double, along with the type information that it is a Date (the Variant's Type property = 7).

Although Date is a class that is subclassed from Object, VarType identifies a Date as a Date data type (Type=7) rather than an Object (Type=9).

Use the ParseDate function to convert a date string to a Date value.

The TotalSeconds property is the “master” property that stores the date/time associated with a Date. The other property values are derived from TotalSeconds. If you change the value of the TotalSeconds property, the values of the Year, Month, Day, Hour, Minute, and Second properties change to reflect the second on which TotalSeconds occurs. Conversely, if you change any of these properties, the value of TotalSeconds changes commensurately.

The Date properties that return formatted date or time information are affected by the user's operating system settings. The user’s system settings control the formats that are used.

You can use the Str function to obtain the string value of the date in SQL date/time format, i.e., the following gets the string value of the current date/time:

Dim d as New Date
MsgBox Str(d)


On Windows, the Regional and Language Options panel determines how dates are formatted. On Macintosh, the date formats are specified in the Formats panel in the International control panel.

Regional settings in Vista.
International settings on Macintosh.

If you need to control the exact appearance of date/time information, the best way is to extract the information yourself and manage the formatting using string manipulation functions.

The ParseDate function accepts a date as a string and converts it to a Date object. It accepts only date strings that are in any of the formats specified by the user's system settings.

Examples

This example creates a Date object and sets it to 15 April, 2009.

Dim d as New Date(2009,4,15)


This example creates a Date and displays the current date in a message box.

Dim d as Date
d=New Date
MsgBox d.shortdate


You can write this more compactly by instantiating the Date variable within the Dim statement, as in this example:

Dim d as New Date
MsgBox d.shortdate


This example sets a Date object to 10 February 1954.

Dim d as New Date
d.Year=1954
d.Month=2
d.Day=10
MsgBox d.ShortDate


The following example compares that date to the current date:

Dim d as New Date
Dim today as New Date
Dim i as Integer
d.Year=1954
d.Month=2
d.Day=10
i=today.Operator_Compare(d)


See Also

Microseconds, ParseDate, Str, Ticks functions; FolderItem class.

Personal tools