Extract Each Part of a Date


Date objects are based on floating-point Double numbers so mathematical operations and comparisons can be used with Date objects. The Date and Time functions, however, return strings, so they can't be used in this capacity. OOo Basic provides functions to retrieve the individual pieces of a date (see Table 5 ).

Table 5: Date component extraction functions in OOo Basic.

Function

Description

Year

Return the year portion of a Date value as an Integer.

Month

Return the month portion of a Date value as an Integer.

Day

Return the day portion of a Date value as an Integer from a Date value.

Hour

Return the hour portion of a Date value as an Integer from a Date value.

Minute

Return the minutes portion of a Date value as an Integer from a Date value.

Second

Return the seconds portion of a Date value as an Integer from a Date value.

WeekDay

Return an integer value from 1 through 7, corresponding to the day of the week, Sunday through Saturday, for a Date value.

The functions in Table 5g all expect a Date object, which is internally based on a Double. OOo Basic automatically converts the argument to the appropriate type if possible. The Date function returns a string with no time information (everything to the right of the decimal is zero) so there is no time information for the Hour, Minute, and Second functions to return. Similarly, the Time function returns a string with no date information (everything to the left of the decimal is zero), which corresponds to December 30, 1899.

 Print "Year = " & Year(0.223)      '1899, 0 for date means December 30, 1899 Print "Year = " & Year(Time)       '1899, No date information from Time() Print "Month = " & Month(Date)     'Current month Print "Day = " & Day(Now)          'Now contains date and time information Print "Hour = " & Hour(Date)       '0, No time information from Date() Print "Minutes = " & Minute(Now)   'Current minutes Print "Seconds = " & Second(Time)  'Current seconds 

Use the WeekDay function to determine the day of the week. Some calendars start on Monday and some start on Sunday; OOo Basic assumes that Sunday is the first day of the week. See Listing 9 and Figure 2 .


Figure 2: Use WeekDay to determine the day of the week.
Listing 9: WeekDayText is found in the Date module in this chapter's source code files as SC05.sxw.
start example
 Sub ExampleWeekDayText   Print "Today is " & WeekDayText(Date) End Sub Function WeekDayText(d) As String   Select Case WeekDay(d)     case 1       WeekDayText="Sunday"     case 2       WeekDayText="Monday"     case 3       WeekDayText="Tuesday"     case 4       WeekDayText="Wednesday"     case 5       WeekDayText="Thursday"     case 6       WeekDayText="Friday"     case 7       WeekDayText="Saturday"   End Select End Function 
end example
 



OpenOffice.org Macros Explained
OpenOffice.org Macros Explained
ISBN: 1930919514
EAN: 2147483647
Year: 2004
Pages: 203

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net